Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Fix: Product Collection show products with correct stock statuses #11708

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/BlockTypes/ProductCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ private function get_final_query_args( $common_query_values, $query, $is_exclude
$orderby_query = $query['orderby'] ? $this->get_custom_orderby_query( $query['orderby'] ) : [];
$on_sale_query = $this->get_on_sale_products_query( $query['on_sale'] );
$stock_query = $this->get_stock_status_query( $query['stock_status'] );
$visibility_query = is_array( $query['stock_status'] ) ? $this->get_product_visibility_query( $stock_query ) : [];
$visibility_query = is_array( $query['stock_status'] ) ? $this->get_product_visibility_query( $stock_query, $query['stock_status'] ) : [];
$featured_query = $this->get_featured_query( $query['featured'] ?? false );
$attributes_query = $this->get_product_attributes_query( $query['product_attributes'] );
$taxonomies_query = $query['taxonomies_query'] ?? [];
Expand Down Expand Up @@ -612,16 +612,17 @@ private function get_stock_status_query( $stock_statuses ) {
/**
* Return a query for product visibility depending on their stock status.
*
* @param array $stock_query Stock status query.
* @param array $stock_query Stock status query.
* @param array $stock_status Selected stock status.
*
* @return array Tax query for product visibility.
*/
private function get_product_visibility_query( $stock_query ) {
private function get_product_visibility_query( $stock_query, $stock_status ) {
$product_visibility_terms = wc_get_product_visibility_term_ids();
$product_visibility_not_in = array( is_search() ? $product_visibility_terms['exclude-from-search'] : $product_visibility_terms['exclude-from-catalog'] );

// Hide out of stock products.
if ( empty( $stock_query ) && 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
if ( empty( $stock_query ) && ! in_array( 'outofstock', $stock_status, true ) ) {
$product_visibility_not_in[] = $product_visibility_terms['outofstock'];
}

Expand Down
29 changes: 0 additions & 29 deletions tests/php/BlockTypes/ProductCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,35 +251,6 @@ public function test_product_visibility_query_exist_in_merged_query() {
),
$merged_query['tax_query']
);

$fn = function() {
return 'yes';
};

// Test with hide out of stock items option enabled.
add_filter(
'pre_option_woocommerce_hide_out_of_stock_items',
$fn
);
$product_visibility_not_in[] = $product_visibility_terms['outofstock'];

$parsed_block = $this->get_base_parsed_block();

$merged_query = $this->initialize_merged_query( $parsed_block );

$this->assertContainsEquals(
array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_not_in,
'operator' => 'NOT IN',
),
$merged_query['tax_query']
);
remove_filter(
'pre_option_woocommerce_hide_out_of_stock_items',
$fn
);
}

/**
Expand Down
Loading