Automattic\WooCommerce\Blocks\BlockTypes

ProductFilterAttribute::prepare_selected_filters()publicWC 1.0

Prepare the active filter items.

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

Хуков нет.

Возвращает

Массив. Active filters items.

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

$ProductFilterAttribute = new ProductFilterAttribute();
$ProductFilterAttribute->prepare_selected_filters( $items, $params );
$items(массив) (обязательный)
The active filter items.
$params(массив) (обязательный)
The query param parsed from the URL.

Код ProductFilterAttribute::prepare_selected_filters() WC 9.8.5

public function prepare_selected_filters( $items, $params ) {
	$product_attributes_map = array_reduce(
		wc_get_attribute_taxonomies(),
		function ( $acc, $attribute_object ) {
			$acc[ $attribute_object->attribute_name ] = $attribute_object->attribute_label;
			return $acc;
		},
		array()
	);

	$active_product_attributes = array_reduce(
		array_keys( $params ),
		function ( $acc, $attribute ) {
			if ( strpos( $attribute, 'filter_' ) === 0 ) {
				$acc[] = str_replace( 'filter_', '', $attribute );
			}
			return $acc;
		},
		array()
	);

	$active_product_attributes = array_filter(
		$active_product_attributes,
		function ( $item ) use ( $product_attributes_map ) {
			return in_array( $item, array_keys( $product_attributes_map ), true );
		}
	);

	foreach ( $active_product_attributes as $product_attribute ) {
		if ( empty( $params[ "filter_{$product_attribute}" ] ) ) {
			continue;
		}
		$terms           = explode( ',', $params[ "filter_{$product_attribute}" ] );
		$attribute_label = wc_attribute_label( "pa_{$product_attribute}" );

		// Get attribute term by slug.
		foreach ( $terms as $term ) {
			$term_object = get_term_by( 'slug', $term, "pa_{$product_attribute}" );
			$items[]     = array(
				'type'      => 'attribute',
				'value'     => $term,
				'label'     => $attribute_label . ': ' . $term_object->name,
				'attribute' => array(
					'slug'      => $product_attribute,
					'queryType' => 'or',
				),
			);
		}
	}

	return $items;
}