How to get variation descriptions with Woocommerce Rest API?

Viewed 30

In a project I'm working on Woocommerce, I need to get all products and do some operations using PHP and Rest API.

I have successfully listed all of the products. Product descriptions are required in the project I'm working on. I can get these explanations for simple products. There is no problem now. But I can't get the descriptions of the variant products. I reviewed this document: https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products However, I couldn't find a solution. I've been dealing with this problem for several hours. I did some research. However, I couldn't find a similar question.

require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

$woocommerce = new Client(
  'http://example.com',
  'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  [
    'version' => 'wc/v3',
  ]
);

//Get all product
$page = 1;
$products = [];
$all_products = [];
do{
    $products = $woocommerce->get('products',
        array(
            'per_page'  => 100, 
            'page'      => $page,
        )
    );
    $all_products = array_merge($all_products,$products);
    $page++;
} 
while (count($products) > 0);
foreach($all_products as $all_product):
    if($all_product->type =='simple'){
        echo $all_product->description; echo '<pre></pre>';
    }
    elseif($all_product->type =='variable'){
        //?
    }
    //echo '<pre>'; print_r($all_product); echo '</pre>';
endforeach;
0 Answers
Related