Fixed `$post__in` Merging

When the intersection is empty it was returning all products. This
makes it so that it returns nothing when there's no results.
This commit is contained in:
Christopher Allford 2024-09-16 22:12:16 -07:00
parent 1c055f4d28
commit 1fa3ea32d5
No known key found for this signature in database
GPG Key ID: 80E44C778F08A88E
2 changed files with 22 additions and 1 deletions

View File

@ -990,6 +990,11 @@ class ProductCollection extends AbstractBlock {
// has otherwise excluded from the results.
if ( count( $post__in ) > 1 ) {
$post__in = array_intersect( ...$post__in );
// An empty array means that there was no overlap between the filters and so
// the query should return no results.
if ( empty( $post__in ) ) {
return array( -1 );
}
} else {
$post__in = reset( $post__in );
}

View File

@ -975,7 +975,7 @@ class ProductCollection extends \WP_UnitTestCase {
/**
* Test merging exclusive id filters.
*/
public function test_merges_exclusive_id_filters() {
public function test_merges_post__in() {
$existing_id_filter = array( 1, 4 );
$handpicked_product_ids = array( 3, 4, 5, 6 );
// The only ID present in ALL of the exclusive filters is 4.
@ -994,6 +994,22 @@ class ProductCollection extends \WP_UnitTestCase {
$this->assertCount( 1, $merged_query['post__in'] );
}
/**
* Test merging exclusive id filters with no intersection.
*/
public function test_merges_post__in_empty_result_without_intersection() {
$existing_id_filter = array( 1, 4 );
$handpicked_product_ids = array( 2, 3 );
$parsed_block = $this->get_base_parsed_block();
$parsed_block['attrs']['query']['post__in'] = $existing_id_filter;
$parsed_block['attrs']['query']['woocommerceHandPickedProducts'] = $handpicked_product_ids;
$merged_query = $this->initialize_merged_query( $parsed_block );
$this->assertEquals( array( -1 ), $merged_query['post__in'] );
}
/**
* Test for frontend collection handlers.
*/