Automattic\WooCommerce\Blocks\BlockTypes

ProductFilterAttribute::register_active_filters_data()publicWC 1.0

Register the active filters data.

Method of the class: ProductFilterAttribute{}

No Hooks.

Return

Array. Active filters data.

Usage

$ProductFilterAttribute = new ProductFilterAttribute();
$ProductFilterAttribute->register_active_filters_data( $data, $params );
$data(array) (required)
The active filters data.
$params(array) (required)
The query param parsed from the URL.

ProductFilterAttribute::register_active_filters_data() code WC 8.9.0

public function register_active_filters_data( $data, $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 );
		}
	);

	$action_namespace = $this->get_full_block_name();

	foreach ( $active_product_attributes as $product_attribute ) {
		$terms = explode( ',', get_query_var( "filter_{$product_attribute}" ) );

		// Get attribute term by slug.
		$terms = array_map(
			function( $term ) use ( $product_attribute, $action_namespace ) {
				$term_object = get_term_by( 'slug', $term, "pa_{$product_attribute}" );
				return array(
					'title'      => $term_object->name,
					'attributes' => array(
						'data-wc-on--click' => "$action_namespace::actions.removeFilter",
						'data-wc-context'   => "$action_namespace::" . wp_json_encode(
							array(
								'value'         => $term,
								'attributeSlug' => $product_attribute,
								'queryType'     => get_query_var( "query_type_{$product_attribute}" ),
							)
						),
					),
				);
			},
			$terms
		);

		$data[ $product_attribute ] = array(
			'type'  => $product_attributes_map[ $product_attribute ],
			'items' => $terms,
		);
	}

	return $data;
}