Automattic\WooCommerce\Caches

ProductCountCacheService::update_on_new_productpublicWC 1.0

Update the cache when a new product is created.

Method of the class: ProductCountCacheService{}

No Hooks.

Returns

null. Nothing (null).

Usage

$ProductCountCacheService = new ProductCountCacheService();
$ProductCountCacheService->update_on_new_product( $product_id, $product ): void;
$product_id(int) (required)
Product ID.
$product(WC_Product) (required)
The product.

ProductCountCacheService::update_on_new_product() code WC 11.0.0

public function update_on_new_product( int $product_id, WC_Product $product ): void {
	// transition_post_status already counted this product — reverse any errant decrement from a cold step 1 and stop.
	// In-memory status may diverge from DB after a mid-creation wp_update_post; do not increment here.
	if ( isset( $this->product_statuses[ $product_id ] ) ) {
		$this->maybe_restore_initial_status_count( $product_id );
		unset( $this->products_in_creation[ $product_id ] );
		return;
	}

	// Cache was cold throughout creation — transition_post_status never fired; use in-memory status as the sole count.
	$product_status = $product->get_status();
	if ( $this->product_count_cache->is_cached( 'product', $product_status ) ) {
		$this->product_statuses[ $product_id ] = $product_status;
		$this->product_count_cache->increment( 'product', $product_status );
	}
	unset( $this->products_in_creation[ $product_id ] );
}