Automattic\WooCommerce\Internal\ProductFilters
QueryClauses::get_chosen_attributes
Get an array of attributes and terms selected from query arguments.
Method of the class: QueryClauses{}
No Hooks.
Returns
Array.
Usage
// private - for code of main (parent) class only $result = $this->get_chosen_attributes( $query_vars ): array;
- $query_vars(array) (required)
- The WP_Query arguments.
QueryClauses::get_chosen_attributes() QueryClauses::get chosen attributes code WC 10.3.3
private function get_chosen_attributes( array $query_vars ): array {
$chosen_attributes = array();
if ( empty( $query_vars ) ) {
return $chosen_attributes;
}
foreach ( $query_vars as $key => $value ) {
if ( 0 === strpos( $key, 'filter_' ) ) {
$attribute = wc_sanitize_taxonomy_name( str_replace( 'filter_', '', $key ) );
$taxonomy = wc_attribute_taxonomy_name( $attribute );
$filter_terms = ! empty( $value ) ? explode( ',', wc_clean( wp_unslash( $value ) ) ) : array();
if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) || ! wc_attribute_taxonomy_id_by_name( $attribute ) ) {
continue;
}
$query_type = ! empty( $query_vars[ 'query_type_' . $attribute ] ) && in_array( $query_vars[ 'query_type_' . $attribute ], array( 'and', 'or' ), true ) ? wc_clean( wp_unslash( $query_vars[ 'query_type_' . $attribute ] ) ) : '';
$chosen_attributes[ $taxonomy ]['terms'] = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding.
$chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : 'and';
}
}
return $chosen_attributes;
}