WC_Product_Data_Store_CPT::clear_cachesprotectedWC 3.0.0

Clear any caches.

Method of the class: WC_Product_Data_Store_CPT{}

No Hooks.

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->clear_caches( $product );
$product(WC_Product) (required) (passed by reference — &)
Product object.

Changelog

Since 3.0.0 Introduced.

WC_Product_Data_Store_CPT::clear_caches() code WC 10.8.1

protected function clear_caches( &$product ) {
	wc_delete_product_transients( $product->get_id() );
	if ( $product->get_parent_id( 'edit' ) ) {
		wc_delete_product_transients( $product->get_parent_id( 'edit' ) );
		WC_Cache_Helper::invalidate_cache_group( 'product_' . $product->get_parent_id( 'edit' ) );
	}
	WC_Cache_Helper::invalidate_attribute_count( array_keys( $product->get_attributes() ) );
	WC_Cache_Helper::invalidate_cache_group( 'product_' . $product->get_id() );

	// Clear product object cache last, as operations above may call wc_get_product() and re-cache.
	if ( \Automattic\WooCommerce\Utilities\FeaturesUtil::feature_is_enabled( 'product_instance_caching' ) ) {
		$product_cache = wc_get_container()->get( ProductCache::class );
		$product_cache->remove( $product->get_id() );

		// Also clear parent's cache if this is a variation, as operations above may have cached
		// the parent with stale children data.
		$parent_id = $product->get_parent_id( 'edit' );
		if ( $parent_id ) {
			$product_cache->remove( $parent_id );
		}
	}
}