Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions

VariationSelectorAttribute::build_variation_selectable_itemsprivateWC 1.0

Build selectable items for the inner block protocol and client context.

Method of the class: VariationSelectorAttribute{}

No Hooks.

Returns

Array.

Usage

// private - for code of main (parent) class only
$result = $this->build_variation_selectable_items( $attribute_name, $attribute_slug, $attribute_terms, ?string $default_selected ): array;
$attribute_name(string) (required)
Product attribute name.
$attribute_slug(string) (required)
Attribute slug.
$attribute_terms(array) (required)
Terms from context.
?string $default_selected(required)
.

VariationSelectorAttribute::build_variation_selectable_items() code WC 10.9.1

private function build_variation_selectable_items( string $attribute_name, string $attribute_slug, array $attribute_terms, ?string $default_selected ): array {
	$id_prefix    = sanitize_title( $attribute_slug );
	$items        = array();
	$term_visuals = VisualAttributeTermMeta::is_visual_attribute_taxonomy( $attribute_name )
		? VisualAttributeTermMeta::get_term_visuals( wp_list_pluck( $attribute_terms, 'term_id' ) )
		: array();

	foreach ( $attribute_terms as $attribute_term ) {
		if ( ! is_array( $attribute_term ) || ! isset( $attribute_term['value'], $attribute_term['label'] ) ) {
			continue;
		}
		$value = (string) $attribute_term['value'];
		$slug  = sanitize_title( $value );
		$item  = array(
			'id'        => $id_prefix . '-' . $slug,
			'label'     => (string) $attribute_term['label'],
			'value'     => $value,
			'ariaLabel' => (string) $attribute_term['label'],
			'selected'  => $default_selected === $value,
		);

		if ( isset( $attribute_term['term_id'], $term_visuals[ $attribute_term['term_id'] ] ) ) {
			$item['visual'] = $term_visuals[ $attribute_term['term_id'] ];
		}

		$items[] = $item;
	}

	return $items;
}