I have a list of products which should show variations in it's own row. The code for that works fine so far.
But I couldn't figure out how I could show the SKU of a variation.
Here's my current code:
$args = [
'status' => array('publish', 'draft'),
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
];
$vendor_products = wc_get_products($args);
$list_array = array();
foreach ($vendor_products as $key => $product) {
if ($product->get_type() == "variable") {
foreach ($product->get_variation_attributes() as $variations) {
foreach ($variations as $variation) {
$list_array[] = array(
'SKU' => $product->get_sku(),
'Name' => $product->get_title() . " - " . $variation,
);
}
}
} else {
$list_array[] = array(
'SKU' => $product->get_sku(),
'Name' => $product->get_title(),
);
}
}
return $list_array;
I tried to display the product attributes and I also tried to get the ID of the variation. Nothing works for me?!
Is there no simple way to get the variation SKU?