Fix price filtering when there are variations.

The price filtering query wasn't working properly when there are
variations with different prices: if at least one variation was
outside of the price range but other were inside, the product wasn't
being listed.
This commit is contained in:
Nestor Soriano 2021-02-05 16:34:42 +01:00
parent e5a3ac48aa
commit 3976abffd6
2 changed files with 38 additions and 3 deletions

View File

@ -622,9 +622,9 @@ class WC_Query {
$args['join'] = $this->append_product_sorting_table_join( $args['join'] );
$args['where'] .= $wpdb->prepare(
' AND wc_product_meta_lookup.min_price >= %f AND wc_product_meta_lookup.max_price <= %f ',
$current_min_price,
$current_max_price
' AND NOT (%f<wc_product_meta_lookup.min_price OR %f>wc_product_meta_lookup.max_price ) ',
$current_max_price,
$current_min_price
);
return $args;
}

View File

@ -0,0 +1,35 @@
<?php
/**
* Tests for WC_Query.
*/
class WC_Query_Test extends \WC_Unit_Test_Case {
/**
* @testdox 'price_filter_post_clauses' generates the proper 'where' clause when there are 'max_price' and 'min_price' arguments in the query.
*/
public function test_price_filter_post_clauses_creates_the_proper_where_clause() {
// phpcs:disable Squiz.Commenting
$wp_query = new class() {
public function is_main_query() {
return true;
}
};
// phpcs:enable Squiz.Commenting
$_GET['min_price'] = '100';
$_GET['max_price'] = '200';
$sut = new WC_Query();
$args = array(
'join' => '(JOIN CLAUSE)',
'where' => '(WHERE CLAUSE)',
);
$args = $sut->price_filter_post_clauses( $args, $wp_query );
$expected = '(WHERE CLAUSE) AND NOT (200.000000<wc_product_meta_lookup.min_price OR 100.000000>wc_product_meta_lookup.max_price ) ';
$this->assertEquals( $expected, $args['where'] );
}
}