diff --git a/src/BlockTypes/ProductCollection.php b/src/BlockTypes/ProductCollection.php index 45056369897..c3e627a30e7 100644 --- a/src/BlockTypes/ProductCollection.php +++ b/src/BlockTypes/ProductCollection.php @@ -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'] ?? []; @@ -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']; } diff --git a/tests/php/BlockTypes/ProductCollection.php b/tests/php/BlockTypes/ProductCollection.php index 5007f28960b..2d3d32f64df 100644 --- a/tests/php/BlockTypes/ProductCollection.php +++ b/tests/php/BlockTypes/ProductCollection.php @@ -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 - ); } /**