Automattic\WooCommerce\StoreApi\Routes\V1\Ucp

AbstractCatalogRoute::resolve_product │ protected │ WC 1.0

Resolve a catalog product by product or variation ID.

The lookup schema requires variant identifiers to be resolvable, so an identifier naming a variation resolves to its parent product — the variation is returned as one of that product's variants, not as a self-parented product of its own. Callers are told which variation was named so they can correlate it back to the request.

Метод класса: AbstractCatalogRoute{}

Хуков нет.

Возвращает

array|null. { product, variation_id }, where variation_id is 0 when the identifier named the product itself, or null when nothing catalog-visible matched.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->resolve_product( $identifier );
$identifier(строка) (обязательный)
Product ID or variation ID.

Код AbstractCatalogRoute::resolve_product() WC 11.3.0

protected function resolve_product( string $identifier ) {
	$product_id   = ctype_digit( $identifier ) ? (int) $identifier : 0;
	$product      = $product_id > 0 ? wc_get_product( $product_id ) : null;
	$variation_id = 0;

	if ( $product instanceof \WC_Product_Variation ) {
		$variation_id = (int) $product->get_id();
		$product      = wc_get_product( $product->get_parent_id() );
	}

	// Catalog visibility as the storefront applies it: published, not hidden
	// from the catalog, and in stock when the store hides out-of-stock items.
	if ( ! $product instanceof \WC_Product || ! $product->is_visible() ) {
		return null;
	}

	if ( ! $this->mapper()->has_catalog_variants( $product ) ) {
		return null;
	}

	// A named variation the storefront would not offer — disabled, or out of
	// stock while the store hides out-of-stock items — is a miss even when
	// its parent is otherwise fine.
	if ( $variation_id > 0 && ( ! $product instanceof \WC_Product_Variable || ! in_array( $variation_id, array_map( 'intval', $product->get_visible_children() ), true ) ) ) {
		return null;
	}

	return array(
		'product'      => $product,
		'variation_id' => $variation_id,
	);
}