• Resolved 21stcn

    (@21stcn)


    I cannot figure out how to disable/enable a product variation using the WooCommerce rest api (php). I simply want to replicate the Enable checkbox functionality found when editing a variation through the dashboard, so basically prevent users from purchasing the variation. Using the dashboard checkbox achieves all this, so why can’t I do it using the rest api?

    The only public property I can think to change would be ‘purchasable’. So I would have thought that to disable a product variation, this would work (from the docs https://woocommerce.github.io/woocommerce-rest-api-docs/#update-a-product-variation):

    
    $woocommerce->put(
        'products/103/variations/109',
        ['purchasable' => false]
     );

    Assuming the product and variation id are correct, which I have tested easily by changing another property like price, why am I not able to change this property?

    How am I meant to enable/disable a product variation using the rest API (without using the ‘hack’ of managing stock — I am not managing stock as products are digital)? It seems like such a basic requirement but this value will not change. Even setting the ‘parent’ product to draft does not disable its variations. Thanks for your help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Howdy ??

    The checkbox coincides with the post status of the variation but as near as I can tell, that property is not supported by the REST API as it is not listed in the documentation here: https://woocommerce.github.io/woocommerce-rest-api-docs/#product-variation-properties

    Which is another way to say that it appears to be working as designed.

    I would suggest you report this on the github issue tracker for WooCommerce and see if support can be added for enabling/disabling product variations via the REST API. You can find the issue tracker here: https://github.com/woocommerce/woocommerce/issues

    Thread Starter 21stcn

    (@21stcn)

    Thanks for your reply and pointing me in the direction of the list of properties in the documentation that can be changed using the rest api. I see now that purchasable is ‘read-only’ which would have saved me some time.

    I wonder if simply setting the variation post itself as a draft is the way to handle this programmatically, using wp_update_post()? In testing, setting the variation post to draft:

    1. Removes it from the user’s cart
    2. Removes it from the product page

    The problem with this method is that, after setting the variation as draft, the variation does not have ‘Enable’ unchecked in the dashboard when editing the parent product, it simply disappears. So unless you’re handling everything programmatically, this is potentially dangerous.

    Ok guess I’ll open a github issue as there should be a way to toggle the purchasable property using the rest api.

    jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    @21stcn The other hacky way to make a variation not purchasable, but still editable in the admin would be to remove the price of the variation (null, not 0).

    Thread Starter 21stcn

    (@21stcn)

    https://github.com/woocommerce/woocommerce/issues/20374

    It seems that updating the ‘visible’ property of a single variation via the API toggles the ‘enabled’ checkbox, so it has the same functionality. It sets purchasable to false (indirectly).

    For example, if you toggle the ‘visible’ property of a single variation to false using the rest api, $woocommerce->put(‘products/22/variations/733’, $data);, it will result in ‘purchasable’ being set to false, and uncheck the ‘Enable’ checkbox.

    However, if the parent product is not also set to invisible, the variation visibility will remain true, even though purchasable is set to false. So this seems the best way to do it, even though it is kind of counter intuitive.

    This works:

    $woocommerce->put('products/22/variations/23, $data);
    
    $data = [
        'visible' => false
    ];

    Resulting in these properties for the variation:

    
    if parent visible: 'visible' = true; 'purchasable' = false;
    if parent not visible/draft: 'visible' = false; 'purchasable' = false

    Note however, just for the record, this same behavior does not occur when batching:

    $woocommerce->post('products/22/variations/batch', $data);

    Batch changing variation visibility does not change variation purchasable property nor toggle enable checkbox.

    • This reply was modified 6 years, 5 months ago by 21stcn.
    • This reply was modified 6 years, 5 months ago by 21stcn.
    • This reply was modified 6 years, 5 months ago by 21stcn.
    jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    @21stcn That’s great that you were able to determine a way to do what was needed. Would you consider the batching behavior to be a bug, and if so, did you create a bug report for it?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How enable/disable WooCommerce product variation using rest API (php)?’ is closed to new replies.