Automattic\WooCommerce\Internal\ShopperLists

ShopperListItem::from_productpublic staticWC 1.0

Construct from a product (or variation) ID and optional payload fields.

Method of the class: ShopperListItem{}

No Hooks.

Returns

self|null. Null if the underlying product can't be resolved or isn't published.

Usage

$result = ShopperListItem::from_product( $product_or_variation_id, $variation, $quantity ): ?self;
$product_or_variation_id(int) (required)
Product or variation ID.
$variation(array)
Variation attributes keyed by attribute name.
Default: array()
$quantity(int)
Saved quantity. Coerced to a minimum of 1.
Default: 1

ShopperListItem::from_product() code WC 10.9.1

public static function from_product( int $product_or_variation_id, array $variation = array(), int $quantity = 1 ): ?self {
	$product = wc_get_product( absint( $product_or_variation_id ) );
	if ( ! $product || ! self::product_is_live( $product ) ) {
		return null;
	}

	if ( $product->is_type( ProductType::VARIATION ) ) {
		$variation_id = $product->get_id();
		$product_id   = $product->get_parent_id();
		$variation    = self::resolve_variation_attributes( $product, $variation );
	} elseif ( $product->is_type( ProductType::VARIABLE ) ) {
		throw new \InvalidArgumentException(
			esc_html__( 'When saving a variation, product_id must be the variation ID, not the parent product ID.', 'woocommerce' )
		);
	} else {
		$product_id   = $product->get_id();
		$variation_id = 0;
		$variation    = array();
	}

	return new self(
		self::generate_key( $product_id, $variation_id, $variation ),
		$product_id,
		$variation_id,
		$variation,
		max( 1, $quantity ),
		current_time( 'mysql', true ),
		$product->get_title()
	);
}