Advertisement
borkolivic

Change visibility setting for out of stock products

Feb 2nd, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.74 KB | None | 0 0
  1. /* Change product visibility if out-of-stock  */
  2. add_action( 'pre_get_posts', 'mx_hide_outofstock_products' );
  3.  
  4. function mx_hide_outofstock_products( $q ) {
  5.  
  6.     if ( ! $q->is_main_query() || is_admin() ) {
  7.         return;
  8.     }
  9.  
  10.     if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) {
  11.  
  12.         $tax_query = (array) $q->get('tax_query');
  13.  
  14.         $tax_query[] = array(
  15.             'taxonomy' => 'product_visibility',
  16.             'field' => 'term_taxonomy_id',
  17.             'terms' => array( $outofstock_term->term_taxonomy_id ),
  18.             'operator' => 'NOT IN'
  19.         );
  20.  
  21.         $q->set( 'tax_query', $tax_query );
  22.  
  23.     }
  24.  
  25.     remove_action( 'pre_get_posts', 'mx_hide_outofstock_products' );
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement