Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions

VariationSelectorAttribute::get_termsprotectedWC 1.0

Get product attributes terms.

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

Хуки из метода

Возвращает

Массив[]. Array of term data with structure: [ 'label' => (string) Display label for the term. 'value' => (string) Internal value/slug for the term. 'isSelected' => (bool) Whether this term is the default selection. ]

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_terms( $attribute_name, $attribute_terms );
$attribute_name(строка) (обязательный)
Product Attribute Name.
$attribute_terms(массив) (обязательный)
Product Attribute Terms.

Код VariationSelectorAttribute::get_terms() WC 10.3.6

protected function get_terms( $attribute_name, $attribute_terms ) {
	global $product;

	$is_taxonomy = taxonomy_exists( $attribute_name );

	$selected_attribute = $product->get_variation_default_attribute( $attribute_name );

	if ( $is_taxonomy ) {
		$items = array_map(
			function ( $term ) use ( $attribute_name, $product, $selected_attribute ) {
				return array(
					'value'      => $term->slug,
					/**
					 * Filter the variation option name.
					 *
					 * @since 9.7.0
					 *
					 * @param string     $option_label    The option label.
					 * @param WP_Term|string|null $item   Term object for taxonomies, option string for custom attributes.
					 * @param string     $attribute_name  Name of the attribute.
					 * @param WC_Product $product         Product object.
					 */
					'label'      => apply_filters(
						'woocommerce_variation_option_name',
						$term->name,
						$term,
						$attribute_name,
						$product
					),
					'isSelected' => $selected_attribute === $term->slug,
				);
			},
			wc_get_product_terms( $product->get_id(), $attribute_name, array( 'fields' => 'all' ) ),
		);
	} else {
		$items = array_map(
			function ( $term ) use ( $attribute_name, $product, $selected_attribute ) {
				return array(
					'value'      => $term,
					/**
					 * Filter the variation option name.
					 *
					 * @since 9.7.0
					 *
					 * @param string     $option_label    The option label.
					 * @param WP_Term|string|null $item   Term object for taxonomies, option string for custom attributes.
					 * @param string     $attribute_name  Name of the attribute.
					 * @param WC_Product $product         Product object.
					 */
					'label'      => apply_filters(
						'woocommerce_variation_option_name',
						$term,
						null,
						$attribute_name,
						$product
					),
					'isSelected' => $selected_attribute === $term,
				);
			},
			$attribute_terms,
		);
	}

	return $items;
}