Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporate Stock Clearing of Cache #5

Open
jordantrizz opened this issue Feb 23, 2022 · 0 comments
Open

Incorporate Stock Clearing of Cache #5

jordantrizz opened this issue Feb 23, 2022 · 0 comments

Comments

@jordantrizz
Copy link
Collaborator

See https://gist.github.com/mrcave/7cdf6707273252de04094d4ed8d0d43c

//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 );
//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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant