Commit c9354dbfdcb for woocommerce
commit c9354dbfdcb8d221cc74ffb71e6937e65823240c
Author: moory-se <95882730+moory-se@users.noreply.github.com>
Date: Mon Nov 23 08:58:14 2026 +0100
Fix lookup counts for other OR attribute selections (#71190)
* Fix lookup counts for other OR attribute selections
* Add changelog for cross-attribute OR filter counts
* Explain cross-attribute OR count and stock constraints
* Preserve term IDs in assertions and index OR count subqueries
* Simplify tests
* Simplify comments
* Fix PHPCS
---------
Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com>
diff --git a/plugins/woocommerce/changelog/fix-cross-attribute-or-counts b/plugins/woocommerce/changelog/fix-cross-attribute-or-counts
new file mode 100644
index 00000000000..ef3874369ac
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-cross-attribute-or-counts
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Respect selections in other OR attribute filters when calculating filter counts with the attribute lookup table.
diff --git a/plugins/woocommerce/src/Internal/ProductAttributesLookup/Filterer.php b/plugins/woocommerce/src/Internal/ProductAttributesLookup/Filterer.php
index df5a6d65c32..4170f338dc1 100644
--- a/plugins/woocommerce/src/Internal/ProductAttributesLookup/Filterer.php
+++ b/plugins/woocommerce/src/Internal/ProductAttributesLookup/Filterer.php
@@ -173,7 +173,7 @@ class Filterer {
$tax_query = new TaxQuery( $tax_query );
if ( $this->filtering_via_lookup_table_is_active() ) {
- $query = $this->get_product_counts_query_using_lookup_table( $tax_query, $meta_query, $taxonomy, $term_ids );
+ $query = $this->get_product_counts_query_using_lookup_table( $tax_query, $meta_query, $taxonomy, $term_ids, $query_type );
} else {
$query = $this->get_product_counts_query_not_using_lookup_table( $tax_query, $meta_query, $term_ids );
}
@@ -257,9 +257,10 @@ class Filterer {
* @param \WP_Meta_Query $meta_query The current main meta query.
* @param string $taxonomy The attribute name to get the term counts for.
* @param string $term_ids The term ids to include in the search.
+ * @param string $query_type The query type for the attribute being counted.
* @return array An array of SQL query parts.
*/
- private function get_product_counts_query_using_lookup_table( $tax_query, $meta_query, $taxonomy, $term_ids ) {
+ private function get_product_counts_query_using_lookup_table( $tax_query, $meta_query, $taxonomy, $term_ids, $query_type ) {
global $wpdb;
$meta_query_sql = $meta_query->get_sql( 'post', $this->lookup_table_name, 'product_or_parent_id' );
@@ -294,29 +295,46 @@ class Filterer {
{$in_stock_clause}";
if ( ! empty( $term_ids ) ) {
+ // The main tax query does not include selected attributes while the lookup table is in use, so they are added here.
$attributes_to_filter_by = \WC_Query::get_layered_nav_chosen_attributes();
if ( ! empty( $attributes_to_filter_by ) ) {
- foreach ( $attributes_to_filter_by as $taxonomy => $data ) {
- if ( 'and' !== $data['query_type'] ) {
+ foreach ( $attributes_to_filter_by as $filter_taxonomy => $data ) {
+ // Skip an attribute's own OR selection so its other terms stay available.
+ if ( 'or' === $data['query_type'] && $filter_taxonomy === $taxonomy && 'or' === $query_type ) {
continue;
}
- $all_terms = get_terms(
+ $all_terms = get_terms(
array(
- 'taxonomy' => $taxonomy,
+ 'taxonomy' => $filter_taxonomy,
'hide_empty' => false,
)
);
- $term_ids_by_slug = wp_list_pluck( $all_terms, 'term_id', 'slug' );
- $and_term_ids = array_values( array_intersect_key( $term_ids_by_slug, array_flip( $data['terms'] ) ) );
+ $term_ids_by_slug = wp_list_pluck( $all_terms, 'term_id', 'slug' );
+ $selected_term_ids = array_values( array_intersect_key( $term_ids_by_slug, array_flip( $data['terms'] ) ) );
- if ( empty( $and_term_ids ) ) {
+ if ( empty( $selected_term_ids ) ) {
continue;
}
- $terms_count = count( $and_term_ids );
- $term_ids_list = '(' . join( ',', $and_term_ids ) . ')';
- // The extra derived table ("SELECT product_or_parent_id FROM") is needed for performance
- // (causes the filtering subquery to be executed only once).
+ $terms_count = count( $selected_term_ids );
+ $term_ids_list = $this->get_term_ids_sql( $selected_term_ids );
+
+ if ( 'or' === $data['query_type'] ) {
+ // Check stock on these rows so an in-stock row for another term cannot match this selection.
+ // Taxonomy lets the query use the lookup index. Term IDs are unique, so it does not change which rows match.
+ // The extra derived table ("SELECT product_or_parent_id FROM") is needed for performance
+ // (causes the filtering subquery to be executed only once).
+ $taxonomy_clause = $wpdb->prepare( 'taxonomy = %s', $filter_taxonomy );
+ $query['where'] .= "
+ AND product_or_parent_id IN ( SELECT product_or_parent_id FROM (
+ SELECT product_or_parent_id
+ FROM {$this->lookup_table_name} lt
+ WHERE {$taxonomy_clause} AND term_id IN {$term_ids_list}
+ {$in_stock_clause}
+ ) temp )";
+ continue;
+ }
+
$query['where'] .= "
AND product_or_parent_id IN ( SELECT product_or_parent_id FROM (
SELECT product_or_parent_id
diff --git a/plugins/woocommerce/tests/php/src/Internal/ProductAttributesLookup/FiltererTest.php b/plugins/woocommerce/tests/php/src/Internal/ProductAttributesLookup/FiltererTest.php
index e452a692e46..ef9b765fcfa 100644
--- a/plugins/woocommerce/tests/php/src/Internal/ProductAttributesLookup/FiltererTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/ProductAttributesLookup/FiltererTest.php
@@ -346,7 +346,7 @@ class FiltererTest extends \WC_Unit_Test_Case {
$main_product_in_stock = ! empty(
array_filter(
$data['variations'],
- function( $variation ) {
+ function ( $variation ) {
return $variation['in_stock'];
}
)
@@ -609,7 +609,11 @@ class FiltererTest extends \WC_Unit_Test_Case {
} finally {
remove_filter( 'woocommerce_get_filtered_term_product_counts_query', $scope_to_owned_products );
}
- $this->assertEqualsCanonicalizing( $expected, $term_counts );
+ $this->assertCount( count( $expected ), $term_counts, 'Only the expected terms should be counted.' );
+ foreach ( $expected as $term_id => $count ) {
+ $this->assertArrayHasKey( $term_id, $term_counts, 'The expected term ID must be present.' );
+ $this->assertSame( $count, $term_counts[ $term_id ], 'The count must belong to the expected term ID.' );
+ }
}
/**
@@ -1597,4 +1601,102 @@ class FiltererTest extends \WC_Unit_Test_Case {
$this->assert_counters( 'Color', $expected_to_be_visible ? array( 'Blue' ) : array() );
$this->assert_counters( 'Size', $expected_to_be_visible ? array( 'Medium' ) : array() );
}
+
+ /**
+ * @testdox Counts respect OR selections on other attributes.
+ *
+ * @testWith ["or"]
+ * ["and"]
+ *
+ * @param string $query_type The query type of the widget being counted.
+ */
+ public function test_counts_respect_other_or_attributes( string $query_type ): void {
+ $this->set_use_lookup_table( true );
+ $this->create_product_attribute( 'FacetColor', array( 'Red', 'Blue', 'Green' ) );
+ $this->create_product_attribute( 'FacetSize', array( 'Small', 'Large' ) );
+ $this->create_simple_product(
+ array(
+ 'FacetColor' => array( 'Red' ),
+ 'FacetSize' => array( 'Small' ),
+ ),
+ true
+ );
+ $this->create_simple_product(
+ array(
+ 'FacetColor' => array( 'Blue' ),
+ 'FacetSize' => array( 'Small' ),
+ ),
+ true
+ );
+ $this->create_simple_product(
+ array(
+ 'FacetColor' => array( 'Green' ),
+ 'FacetSize' => array( 'Large' ),
+ ),
+ true
+ );
+
+ $this->do_product_request( array( 'FacetSize' => array( 'Small' ) ), array( 'FacetSize' => 'or' ) );
+ $this->assert_counters( 'FacetColor', array( 'Red', 'Blue' ), $query_type );
+
+ \WC_Query::reset_chosen_attributes();
+ $this->do_product_request(
+ array(
+ 'FacetSize' => array( 'Small' ),
+ 'FacetColor' => array( 'Red' ),
+ ),
+ array(
+ 'FacetSize' => 'or',
+ 'FacetColor' => 'or',
+ )
+ );
+ $this->assert_counters( 'FacetColor', 'or' === $query_type ? array( 'Red', 'Blue' ) : array( 'Red' ), $query_type );
+
+ unset( $_GET['filter_facetcolor'], $_GET['query_type_facetcolor'] );
+ \WC_Query::reset_chosen_attributes();
+ $this->do_product_request( array( 'FacetSize' => array( 'Large' ) ), array( 'FacetSize' => 'or' ) );
+ $this->assert_counters( 'FacetColor', array( 'Green' ), $query_type );
+ }
+
+ /**
+ * @testdox Other OR selections use the selected variation's stock when counting products.
+ *
+ * @testWith [true, ["Blue"]]
+ * [false, ["Blue", "Green"]]
+ *
+ * @param bool $hide_out_of_stock Whether to hide out-of-stock variations.
+ * @param string[] $expected_colors Colors available after selecting Small.
+ */
+ public function test_other_or_counts_respect_variation_stock( bool $hide_out_of_stock, array $expected_colors ): void {
+ $this->set_use_lookup_table( true );
+ $this->set_hide_out_of_stock_items( $hide_out_of_stock );
+ $this->create_product_attribute( 'FacetColor', array( 'Blue', 'Green' ) );
+ $this->create_product_attribute( 'FacetSize', array( 'Small', 'Medium', 'Large' ) );
+ $this->create_simple_product(
+ array(
+ 'FacetColor' => array( 'Blue' ),
+ 'FacetSize' => array( 'Small' ),
+ ),
+ true
+ );
+ $this->create_variable_product(
+ array(
+ 'variation_attributes' => array( 'FacetSize' => array( 'Small', 'Large' ) ),
+ 'non_variation_attributes' => array( 'FacetColor' => array( 'Green' ) ),
+ 'variations' => array(
+ array(
+ 'in_stock' => false,
+ 'defining_attributes' => array( 'FacetSize' => 'Small' ),
+ ),
+ array(
+ 'in_stock' => true,
+ 'defining_attributes' => array( 'FacetSize' => 'Large' ),
+ ),
+ ),
+ )
+ );
+
+ $this->do_product_request( array( 'FacetSize' => array( 'Small', 'Medium' ) ), array( 'FacetSize' => 'or' ) );
+ $this->assert_counters( 'FacetColor', $expected_colors, 'or' );
+ }
}