1
votes

In WooCommerce I have checked the option "Visibility out of stock", but I need that some product category stay visible, even if that option is checked.

On the other hand, the main store doesn't show that category. For that I used this code:

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {

  if ( ! $q->is_main_query() ) return;
  if ( ! $q->is_post_type_archive() ) return;  
  if ( ! is_admin() && is_shop() ) {

  $q->set( 'tax_query', array(array(
      'taxonomy' => 'product_cat',
      'field' => 'slug',
      'terms' => array( 'MYCATEGORY' ), // Don't show this category.
      'operator' => 'NOT IN'
  )));  
  }

  remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}

As it would be possible to do this?

Thank you!

1

1 Answers

1
votes

This is just a quick guess, not tested but could you try starting with something like:

global $product;

$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;

$category_to_show = ‘MYCATEGORY’;
if( $categories ) {
    if( in_array( $category_to_show, $categories ) && !$product->is_in_stock() ) {
        apply_filters( 'woocommerce_product_is_visible', true, $product->id );
    }
}