Created
May 16, 2020 13:50
-
-
Save Musilda/16a72c08b0d1854a90984beb2d353a69 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 | |
add_action( 'pre_get_posts', 'musilda_hide_out_of_stock_products' ); | |
function musilda_hide_out_of_stock_products( $q ) { | |
if ( ! $q->is_main_query() || is_admin() ) { | |
return; | |
} | |
if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) { | |
$tax_query = (array) $q->get('tax_query'); | |
$tax_query[] = array( | |
'taxonomy' => 'product_visibility', | |
'field' => 'term_taxonomy_id', | |
'terms' => array( $outofstock_term->term_taxonomy_id ), | |
'operator' => 'NOT IN' | |
); | |
$q->set( 'tax_query', $tax_query ); | |
} | |
remove_action( 'pre_get_posts', 'musilda_hide_out_of_stock_products' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment