Automattic\WooCommerce\Internal\ProductFeed\Mapping

UcpProductMapper::map_lookup_product │ public │ WC 1.0

Map a product for POST /catalog/lookup.

Every returned variant must be justified by a request identifier via the required inputs metadata. A product-level identifier resolves to all of the product's variants as featured — server-selected representatives — while an identifier naming a variant directly by id resolves only to that variant, as exact. Variants no identifier resolved to are dropped, so a request naming only variations gets only those variations back; several identifiers resolving to one variant all appear in its inputs.

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

Хуков нет.

Возвращает

array.

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

$UcpProductMapper = new UcpProductMapper();
$UcpProductMapper->map_lookup_product( $product, $inputs ): array;
$product(WC_Product) (обязательный)
WooCommerce product.
$inputs(массив) (обязательный)
Identifiers from the lookup request that resolved to this product, each { id, variation_id } with the variation it named or 0 for the product itself.

Код UcpProductMapper::map_lookup_product() WC 11.3.0

public function map_lookup_product( \WC_Product $product, array $inputs ): array {
	// A variation named by id must survive the per-product cap.
	$required      = array();
	$names_product = false;
	foreach ( $inputs as $input ) {
		if ( $input['variation_id'] > 0 ) {
			$required[] = (int) $input['variation_id'];
			continue;
		}

		// A product-level identifier needs the full variant list.
		$names_product = true;
	}

	// With only variation-level identifiers, build only those variants: the
	// loop below discards the rest anyway.
	$payload = $this->map_product( $product, $required, ! $names_product );

	if ( ! isset( $payload['variants'] ) || ! is_array( $payload['variants'] ) ) {
		return $payload;
	}

	$variants = array();

	foreach ( $payload['variants'] as $variant ) {
		$correlations = array();

		foreach ( $inputs as $input ) {
			// The resolved variation decides the match: the raw id may carry
			// leading zeros the resolver already normalized.
			$names_variant = $input['variation_id'] > 0;

			if ( $names_variant && (int) $input['variation_id'] !== (int) $variant['id'] ) {
				continue;
			}

			$correlations[] = array(
				'id'    => $input['id'],
				'match' => $names_variant ? 'exact' : 'featured',
			);
		}

		if ( empty( $correlations ) ) {
			continue;
		}

		$variant['inputs'] = $correlations;
		$variants[]        = $variant;
	}

	$payload['variants'] = $variants;

	return $payload;
}