Automattic\WooCommerce\Internal\ProductFeed\Mapping

UcpProductMapper::build_options │ private │ WC 1.0

Build product options for variable products.

Lists the values the visible variations actually use, in the attribute's own term order; an "Any" variation offers every term.

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

Хуков нет.

Возвращает

array.

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

// private - только в коде основоного (родительского) класса
$result = $this->build_options( $item ): array;
$item(массив) (обязательный)
Store API item response.

Код UcpProductMapper::build_options() WC 11.3.0

private function build_options( array $item ): array {
	$variations = (array) $item['variations'];
	if ( empty( $variations ) ) {
		return array();
	}

	$used = array();
	$any  = array();
	foreach ( $variations as $variation ) {
		foreach ( (array) $variation['attributes'] as $attribute ) {
			$name = (string) $attribute['name'];
			if ( null === $attribute['value'] || '' === (string) $attribute['value'] ) {
				$any[ $name ] = true;
				continue;
			}
			$used[ $name ][ (string) $attribute['value'] ] = true;
		}
	}

	$options = array();

	foreach ( (array) $item['attributes'] as $attribute ) {
		if ( empty( $attribute['has_variations'] ) ) {
			continue;
		}

		$name   = (string) $attribute['name'];
		$values = array();

		foreach ( (array) $attribute['terms'] as $term ) {
			if ( isset( $any[ $name ] ) || isset( $used[ $name ][ (string) $term['slug'] ] ) ) {
				$values[] = array( 'label' => (string) $term['name'] );
			}
		}

		if ( ! empty( $values ) ) {
			$options[] = array(
				'name'   => $name,
				'values' => $values,
			);
		}
	}

	return $options;
}