Automattic\WooCommerce\Internal\Caches
ProductCache::get
Retrieve a cached product, and if no product is cached with the given id, try to get one via get_from_datastore callback and then cache it.
After retrieval, resets the clone mode to DUPLICATE to maintain backward compatibility for code that expects cloning to clear meta IDs.
Method of the class: ProductCache{}
No Hooks.
Returns
WC_Product|null. Cached product, or null if it's not cached and can't be retrieved from datastore or via callback.
Usage
$ProductCache = new ProductCache(); $ProductCache->get( $id, $expiration, ?callable $get_from_datastore_callback ): ?WC_Product;
- $id(int|string) (required)
- The id of the product to retrieve.
- $expiration(int)
- Expiration of the cached data in seconds from the current time, used if a product is retrieved from datastore and cached.
Default:self::DEFAULT_EXPIRATION - ?callable $get_from_datastore_callback
- .
Default:null
Changelog
| Since 10.5.0 | Introduced. |
ProductCache::get() ProductCache::get code WC 10.8.1
public function get( $id, int $expiration = self::DEFAULT_EXPIRATION, ?callable $get_from_datastore_callback = null ): ?WC_Product {
$id = (int) $id;
$product = parent::get( $id, $expiration, $get_from_datastore_callback );
if ( $product instanceof WC_Product ) {
$product->set_clone_mode( \WC_Data::CLONE_MODE_DUPLICATE );
return $product;
}
return null;
}