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
Changes from 4 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
Loading