Small improvements in the filtering by attribute lookup table.

- Combined two 'if's in one
- Added extra santitization of term ids in the Filterer class
This commit is contained in:
Nestor Soriano 2021-06-08 10:18:35 +02:00
parent c6dff96c0f
commit 0b1158cf5c
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
3 changed files with 24 additions and 20 deletions

View File

@ -766,18 +766,16 @@ class WC_Query {
);
}
if ( ! $this->filterer->filtering_via_lookup_table_is_active() ) {
if ( $main_query && ! $this->filterer->filtering_via_lookup_table_is_active() ) {
// Layered nav filters on terms.
if ( $main_query ) {
foreach ( $this->get_layered_nav_chosen_attributes() as $taxonomy => $data ) {
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $data['terms'],
'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN',
'include_children' => false,
);
}
foreach ( $this->get_layered_nav_chosen_attributes() as $taxonomy => $data ) {
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $data['terms'],
'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN',
'include_children' => false,
);
}
}

View File

@ -74,6 +74,12 @@ class Filterer {
$all_terms = get_terms( $taxonomy, array( 'hide_empty' => false ) );
$term_ids_by_slug = wp_list_pluck( $all_terms, 'term_id', 'slug' );
$term_ids_to_filter_by = array_values( array_intersect_key( $term_ids_by_slug, array_flip( $data['terms'] ) ) );
$term_ids_to_filter_by = array_map(
function( $id ) {
return (int) $id;
},
$term_ids_to_filter_by
);
$term_ids_to_filter_by_list = '(' . join( ',', $term_ids_to_filter_by ) . ')';
$is_and_query = 'and' === $data['query_type'];

View File

@ -21,15 +21,15 @@ class FiltererTest extends \WC_Unit_Test_Case {
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wc_product_attributes_lookup (
product_id bigint(20) NOT NULL,
product_or_parent_id bigint(20) NOT NULL,
taxonomy varchar(32) NOT NULL,
term_id bigint(20) NOT NULL,
is_variation_attribute tinyint(1) NOT NULL,
in_stock tinyint(1) NOT NULL
);
"
CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wc_product_attributes_lookup (
product_id bigint(20) NOT NULL,
product_or_parent_id bigint(20) NOT NULL,
taxonomy varchar(32) NOT NULL,
term_id bigint(20) NOT NULL,
is_variation_attribute tinyint(1) NOT NULL,
in_stock tinyint(1) NOT NULL
);
"
);
$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}wc_product_attributes_lookup" );