Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions

VariationSelectorAttribute::map_term_to_optionprivateWC 1.0

Map a taxonomy term or custom attribute option to the variation row option shape.

Method of the class: VariationSelectorAttribute{}

Hooks from the method

Returns

Array.

Usage

// private - for code of main (parent) class only
$result = $this->map_term_to_option( $term, $attribute_name, $product, $selected_attribute ): array;
$term(WP_Term|string) (required)
Term object for taxonomies, option string for custom attributes.
$attribute_name(string) (required)
Name of the attribute.
$product(WC_Product) (required)
Product object.
$selected_attribute(string) (required)
Default selected attribute value.

VariationSelectorAttribute::map_term_to_option() code WC 10.9.4

private function map_term_to_option( $term, string $attribute_name, \WC_Product $product, string $selected_attribute ): array {
	if ( $term instanceof \WP_Term ) {
		$value       = $term->slug;
		$label       = $term->name;
		$filter_item = $term;
	} elseif ( is_string( $term ) ) {
		$value       = $term;
		$label       = $term;
		$filter_item = null;
	} else {
		return array();
	}

	$option = array(
		'value'      => $value,
		/**
		 * 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',
			$label,
			$filter_item,
			$attribute_name,
			$product
		),
		'isSelected' => $selected_attribute === $value,
	);

	if ( $term instanceof \WP_Term ) {
		$option['term_id'] = $term->term_id;
	}

	return $option;
}