Merge branch 'add/49335-related-products-collection' into add/50860-upsells

This commit is contained in:
Karol Manijak 2024-09-17 16:18:13 +08:00
commit 99dff53412
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.
*/