Automattic\WooCommerce\Internal\ProductFeed\Mapping

UcpProductMapper::match_selection │ private │ WC 1.0

Find the visible variations carrying every selected option value.

Matches on the parent item's variation attributes, so no variation is loaded to be rejected. A label matches a term's name or its slug, case-insensitively, and a variation left as "Any" for an attribute accepts every label, as the storefront does.

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

Хуков нет.

Возвращает

array. Matching variation ids.

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

// private - только в коде основоного (родительского) класса
$result = $this->match_selection( $item, $selected ): array;
$item(массив) (обязательный)
Store API item response of the variable product.
$selected(массив) (обязательный)
Selected option values, each { name, label }.

Код UcpProductMapper::match_selection() WC 11.3.0

private function match_selection( array $item, array $selected ): array {
	$selection = array();
	foreach ( $selected as $option ) {
		if ( isset( $option['name'], $option['label'] ) ) {
			$selection[ strtolower( (string) $option['name'] ) ] = strtolower( (string) $option['label'] );
		}
	}

	if ( empty( $selection ) ) {
		return array();
	}

	$names = $this->term_names( $item );

	// A name no variation attribute carries can never match; without this
	// check the "Any" rule below would accept it on every variation.
	$known = array_fill_keys( array_map( 'strtolower', array_keys( $names ) ), true );
	if ( array_diff_key( $selection, $known ) ) {
		return array();
	}

	$matching = array();

	foreach ( (array) $item['variations'] as $variation ) {
		$values = array();
		foreach ( (array) $variation['attributes'] as $attribute ) {
			if ( null === $attribute['value'] || '' === (string) $attribute['value'] ) {
				continue;
			}

			$name  = (string) $attribute['name'];
			$value = (string) $attribute['value'];

			$values[ strtolower( $name ) ] = array(
				strtolower( $value ),
				strtolower( $names[ $name ][ $value ] ?? $value ),
			);
		}

		foreach ( $selection as $name => $label ) {
			if ( isset( $values[ $name ] ) && ! in_array( $label, $values[ $name ], true ) ) {
				continue 2;
			}
		}

		$matching[] = (int) $variation['id'];
	}

	return $matching;
}