Automattic\WooCommerce\Blocks\BlockTypes
ProductFilterAttribute::prepare_selected_filters
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() ProductFilterAttribute::prepare selected filters WC 10.7.0
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_attributes = array();
$all_term_slugs = array();
$query_types = array();
foreach ( array_keys( $product_attributes_map ) as $attribute_name ) {
$param_key = "filter_{$attribute_name}";
if ( empty( $params[ $param_key ] ) || ! is_string( $params[ $param_key ] ) ) {
continue;
}
// Filter out empty slugs and trim whitespace.
$term_slugs = array_filter(
array_map( 'trim', explode( ',', $params[ $param_key ] ) ),
);
if ( empty( $term_slugs ) ) {
continue;
}
$active_attributes[ "pa_{$attribute_name}" ] = $term_slugs;
$query_types[ $attribute_name ] = $params[ 'query_type_' . $attribute_name ] ?? 'or';
$all_term_slugs = array_merge( $all_term_slugs, $term_slugs );
}
if ( empty( $active_attributes ) ) {
return $items;
}
$attribute_terms = get_terms(
array(
'taxonomy' => array_keys( $active_attributes ),
'slug' => $all_term_slugs,
'hide_empty' => false,
)
);
if ( is_wp_error( $attribute_terms ) || empty( $attribute_terms ) ) {
return $items;
}
foreach ( $attribute_terms as $term_object ) {
$attribute_name = str_replace( 'pa_', '', $term_object->taxonomy );
$items[] = array(
'type' => 'attribute/' . $attribute_name,
'value' => $term_object->slug,
'activeLabel' => sprintf( '%s: %s', $product_attributes_map[ $attribute_name ], $term_object->name ),
'attributeQueryType' => $query_types[ $attribute_name ] ?? 'or',
);
}
return $items;
}