WC_Product_Factory::get_product( mixed $product_id = false, array $deprecated = array() )

Get a product.


Description Description


Parameters Parameters

$product_id

(Optional) WC_Product|WP_Post|int|bool $product Product instance, post instance, numeric or false to use global $post.

Default value: false

$deprecated

(Optional) Previously used to pass arguments to the factory, e.g. to force a type.

Default value: array()


Top ↑

Return Return

(WC_Product|bool) Product object or false if the product cannot be loaded.


Top ↑

Source Source

File: includes/class-wc-product-factory.php

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

		if ( ! $product_id ) {
			return false;
		}

		$product_type = $this->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 = $this->get_classname_from_product_type( $deprecated['product_type'] );
			}
		}

		$classname = $this->get_product_classname( $product_id, $product_type );

		try {
			return new $classname( $product_id, $deprecated );
		} catch ( Exception $e ) {
			return false;
		}
	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.