Automattic\WooCommerce\Blocks\BlockTypes

ProductFilterAttribute::render_attribute_dropdown()privateWC 1.0

Render the dropdown.

Method of the class: ProductFilterAttribute{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->render_attribute_dropdown( $options, $attributes );
$options(array) (required)
Data to render the dropdown.
$attributes(true|false) (required)
Block attributes.

ProductFilterAttribute::render_attribute_dropdown() code WC 9.0.1

private function render_attribute_dropdown( $options, $attributes ) {
	if ( empty( $options ) ) {
		return '';
	}

	$list_items     = array();
	$selected_items = array();

	$product_attribute = wc_get_attribute( $attributes['attributeId'] );

	foreach ( $options as $option ) {
		$item = array(
			'label' => $attributes['showCounts'] ? sprintf( '%1$s (%2$d)', $option['name'], $option['count'] ) : $option['name'],
			'value' => $option['slug'],
		);

		$list_items[] = $item;

		if ( $option['selected'] ) {
			$selected_items[] = $item;
		}
	}

	return Dropdown::render(
		array(
			'items'          => $list_items,
			'action'         => "{$this->get_full_block_name()}::actions.navigate",
			'selected_items' => $selected_items,
			'select_type'    => $attributes['selectType'] ?? 'multiple',
			// translators: %s is a product attribute name.
			'placeholder'    => sprintf( __( 'Select %s', 'woocommerce' ), $product_attribute->name ),
		)
	);
}