Last active
October 25, 2023 12:12
-
-
Save mrcave/7cdf6707273252de04094d4ed8d0d43c to your computer and use it in GitHub Desktop.
Clear NGINX page cache when WooCommerce product inventory levels are changed (only tested on GridPane with Redis page caching enabled)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//clear entire cache when stock is reduced or replenished | |
function clear_cache_when_stock_changes( $order ) { | |
if(class_exists('PhpRedis_Purger') || class_exists('Predis_Purger')){ | |
global $nginx_purger; | |
$nginx_purger->purge_all(); | |
} | |
} | |
add_action( 'woocommerce_reduce_order_stock', 'clear_cache_when_stock_changes', 10, 1 ); | |
add_action( 'woocommerce_restore_order_stock', 'clear_cache_when_stock_changes', 10, 1 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//only clear product page caches when stock changes | |
function clear_product_page_caches_when_stock_changes( $order ) { | |
if(class_exists('PhpRedis_Purger') || class_exists('Predis_Purger')){ | |
if($order){ | |
global $nginx_purger; | |
foreach ( $order->get_items() as $item_id => $item ) { | |
$product_id = $item->get_product_id(); | |
$nginx_purger->purge_post( $product_id ); | |
} | |
} | |
} | |
} | |
add_action( 'woocommerce_reduce_order_stock', 'clear_product_page_caches_when_stock_changes', 10, 1 ); | |
add_action( 'woocommerce_restore_order_stock', 'clear_product_page_caches_when_stock_changes', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ya this hasn't been tested rigorously, but that was actually something I originally had in there and accidentally deleted when troubleshooting. There are a few ways to check for presence of the active plugin. I prefer to look at classes and have added that back in, but you can also check this with is_plugin_active()