Created
November 9, 2023 06:45
-
-
Save dinhtungdu/ef756be0a07d56e66cd892e331426e2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return a query for product visibility depending on their stock status. | |
* | |
* @param array $stock_query Stock status query. | |
* | |
* @return array Tax query for product visibility. | |
*/ | |
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 ) && in_array( 'outofstock', $stock_status, true ) ) { | |
$product_visibility_not_in[] = $product_visibility_terms['outofstock']; | |
} | |
return array( | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'product_visibility', | |
'field' => 'term_taxonomy_id', | |
'terms' => $product_visibility_not_in, | |
'operator' => 'NOT IN', | |
), | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment