keycdn / cache-enabler

A lightweight caching plugin for WordPress that makes your website faster by generating static HTML files.
https://wordpress.org/plugins/cache-enabler/
123 stars 46 forks source link

cache not cleared on WooCommerce stock update after successful order #165

Closed ChessDragon136 closed 3 years ago

ChessDragon136 commented 3 years ago

Hi,

Recently started to use stock management on my WooCommerce site but have noticed that stock numbers are not accurately reflected as the page has not been re-cached on any given user purchase.

Only option selected in plugin setting is: Pre-compress cached pages with Gzip.

Any help appreciated.

Cache Enabler: Version 1.5.5 Wordpress: 5.4.4 WooCommerce: 3.9.2

coreykn commented 3 years ago

Thanks for bringing this up, @ChessDragon136. You're right, the product page cache is not automatically being cleared after the stock has been updated after a successful order. I'm lost at how I thought the new cache clearing for post actions covered this. I must have only been testing this when the stock quantity was manually updated and assumed the same hook was always fired. 😞 I apologize for that. The solution for this will be released shortly.

ChessDragon136 commented 3 years ago

Thanks for looking into this - Thought it was just me.

Can you please implement the hooks into both complete and on-hold, so orders paid by BACS, Cash or some other manual method are also stock reduced at the time of ordering.

woocommerce_payment_complete woocommerce_order_status_on-hold

Rough Coding Outline - NOT TESTED

function on_product_order( $order_id ) { $order = new \WC_Order( $order_id ); $items = $order->get_items(); foreach ( $items as $item ) { $post_id = absint( $item['product_id'] ); self::clear_page_cache_by_post_id( $post_id ); } } add_action( 'woocommerce_payment_complete','on_product_order' ); add_action( 'woocommerce_order_status_on-hold','on_product_order' );

coreykn commented 3 years ago

You're most welcome. I believe I can just use the stock related hooks for this as those are fired whenever the stock is updated, but I won't be certain until I create the solution. I'll make sure the page and associated cache are cleared by default (or the complete cache if the setting is enabled) whenever the stock is automatically updated just like it is when the stock quantity is manually updated. I'll have this ready to be released not next week but the beginning of the week after that (week 49) as I'll be offline next week. I'm sorry for that delay but you can create your own solution in the meantime if you'd like.