Support » Plugin: WooCommerce » Recount terms after updating product stock.

  • Hello,

    Currently I am updating product stock and product status from a supplier API every hour. This works very well.

    However, because I have enabled “Hide products with 0 stock” and “Hide categories with 0 products”, the products count in the categories display does not get updated.

    I can manually force it to recount by pressing the “recount terms” in the status page, but as with everything manual, there’s always room for human error.

    The question is, how can I make it to auto recount terms after every update?

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Do you ‘push’ from the supplier to the WC-REST-API (or using a webhook) or do you ‘pull’ from WooCommerce/WordPress by a scheduled custom function?


    I’d like to say I ‘pull’, but I’m not certain with the terminology.

    Here’s what I do:

    – Hourly scheduled function.
    – Get JSON response from supplier API, parse JSON.
    – For each product, update _stock and _stock_status in $wpdb->postmeta table.

    Yes this is a pull. But writing directly to the database is a bad idea not only to protected data (leading underscore like _stock).

    What you would need to do now it to execute wc_recount_after_stock_change( $product_id ) with each product you have changed the stock for.

    If you would use the new CRUD Classes

    $product = wc_get_product( $product_id );
    $product->set_stock_status( $status );
    $product->set_stock_quantity( $quantity );

    this function would run automatically through the action hook woocommerce_product_set_stock_status.

    Hope this helps

    Can’t use the new classes / functions, as we haven’t updated to WC 3.0 (plugin incompatibility).

    So, I tested the wc_recount_after_stock_change( $product_id ). It seems to work.

    Thank You

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Recount terms after updating product stock.’ is closed to new replies.