Home > Blockchain >  Woocommerce hook that fires after a product stock quantity is updated via REST API (clean cache)
Woocommerce hook that fires after a product stock quantity is updated via REST API (clean cache)

Time:05-26

I hope you can help me with this question I have.

I have Woocommerce that updates the stock quantity via REST API, however I use the caching plugin WP Fastest Cahe, and it only clears the product cache if it is manually updated by WP Admin (backend) when the API updates the amount of stock the plugin does not trigger the product cache clearing. They do however provide a hook for that purpose which is //it clears the post with ID 1923 wpfc_clear_post_cache_by_id(1923); the link that explains it all is this https://www.wpfastestcache.com/tutorial/delete-the-cache-by-calling-the-function/

I am not a programmer but does the code below work? What can I do to trigger the hook when there is the API update ?

Any tips?

I would be very grateful for some help :)

add_action( 'woocommerce_update_product', 'remover_cache_produto', 10, 1 );
function remover_cache_produto($product_id) {
        $product = wc_get_product($product_id);
        
        wpfc_clear_post_cache_by_id($product_id);  //clean cache
    }

CodePudding user response:

You are using the woocommerce_update_product action hook, which fires when you are updating products from backend of wordpress as well from rest api.

So your function with the name remover_cache_produto executes when you are updating products via rest api. You are using the right action hook.

In your function you create a variable $product, which you are not using. So you can get rid of that line.

Put the code into the functions.php file of your theme or create a little wordpress plugin to make it work.

  • Related