WC_Product_Factory::get_productpublicWC 1.0

Get a product.

Method of the class: WC_Product_Factory{}

No Hooks.

Returns

WC_Product|true|false. Product object or false if the product cannot be loaded.

Usage

$WC_Product_Factory = new WC_Product_Factory();
$WC_Product_Factory->get_product( $product_id, $deprecated );
$product_id(mixed)
Product instance, post instance, numeric or false to use global $post.
Default: false
$deprecated(array)
Previously used to pass arguments to the factory, e.g. to force a type.
Default: array()

WC_Product_Factory::get_product() code WC 10.6.2

public function get_product( $product_id = false, $deprecated = array() ) {
	$product_id = (int) $this->get_product_id( $product_id );

	if ( ! $product_id ) {
		return false;
	}

	$use_product_cache = \Automattic\WooCommerce\Utilities\FeaturesUtil::feature_is_enabled( 'product_instance_caching' );
	if ( $use_product_cache && empty( $deprecated ) ) {
		// Nothing should be using the $deprecated argument still, but avoid using cache if they are.
		$product_cache = wc_get_container()->get( ProductCache::class );
		$product       = $product_cache->get( $product_id );
		if ( $product ) {
			return $product;
		}
	}
	_prime_post_caches( array( $product_id ) );

	$product_type = self::get_product_type( $product_id );

	// Backwards compatibility.
	if ( ! empty( $deprecated ) ) {
		wc_deprecated_argument( 'args', '3.0', 'Passing args to the product factory is deprecated. If you need to force a type, construct the product class directly.' );

		if ( isset( $deprecated['product_type'] ) ) {
			$product_type = self::get_classname_from_product_type( $deprecated['product_type'] );
		}
	}

	$classname = self::get_product_classname( $product_id, $product_type );

	try {
		$product = new $classname( $product_id, $deprecated );
		if ( $use_product_cache && isset( $product_cache ) && $product instanceof \WC_Product ) {
			$product_cache->set( $product );
		}
		return $product;
	} catch ( Exception $e ) {
		return false;
	}
}