2

I'm using Wordpress 4.9.5 with Woocommerce 3.3.5. I am using the WooCommerce REST API PHP Client Library to update products on the website when they are changed in a separate product management system. That library is using v2 of the REST API.

Using the following code I am successfully updating the basic product data (title, description, sku, price etc) but I can't get the categories to update from Uncategorized. The categories are also not set when using similar code to create a product if it doesn't already exist on the site.

$client = new WC_API_Client( $domain, $consumerKey, $consumerSecret, $options );
$client->products->update( $id, array( 
'sku' => $product->sku,
'title' => $product->title, 
'type' => $product->type, 
'status' => $product->status,
'regular_price' => $product->regular_price, 
'description' => $product->description,
'categories' => array(
    array( 
        'id' => 343
    ),
    array(
        'id' => 347
    )
)
));

As I say, the other fields update as expected. I have confirmed that categories with IDs 343 and 347 definitely exist so I assume I must have a problem with the syntax. As the other fields update the authentication is definitely working.

I have read the official Woocommerce API documentation and based my code on this tutorial. Based on both of those, I'm not sure what I have done wrong.

Thanks for any help or advice.

1 Answer 1

1

I resolved this in the end. It was on oversight on my part.

The client library I was using was connecting to what the Woocommerce documentation calls the 'Legacy v2' version rather than the 'v2' version of the API. Categories, image alt tags, meta data etc are not supported in the legacy versions.

I switched from the using library to connecting directly to the 'v2' version using https://sitename/wp-json/wc/v2/endpoint and all is now well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.