title: Woocommerce: hide category in shop
body: <p>I have a category with products not visible to everyone.
  I already have a script in place, that removes this category from the sidebar widget, when no items in the category are visible to the user.</p>
  
  <p>I created a function that puts the exclude term_id in a global variable.
  Now I need something that excludes them from view in the shop.</p>
  
  <pre><code>    $GLOBALS['cat_exclude'] = NULL;
  
      function getExcludedCats(  ) {
  
          //if( ! is_admin() &amp;&amp; (is_product_category() || is_shop())){    
  
              $current_tax = get_query_var( 'product_cat' );
              $term =get_term_by( 'slug', $current_tax, 'product_cat');
              $parentid = $term-&gt;term_id;
  
              $args = array(
                  'hide_empty' =&gt; true,
                  'parent' =&gt; $parentid
              );  
  
              $product_categories = get_terms( 'product_cat', $args );
  
              $exclude = array();
              foreach ( $product_categories as $category ) {
  
                  $posts         = get_posts( array( 'post_type' =&gt; 'product', 'posts_per_page' =&gt; -1, 'product_cat' =&gt; $category-&gt;slug, 'fields' =&gt; 'ids' ) );
                  $show_category = false;
  
                  foreach ( $posts as $post ) {
  
                      $product         = new WC_Product( $post );
                      $visible_product = $product-&gt;is_visible();
  
                      if ( true === $visible_product ) {
                          $show_category = true;
                          break;
                      }
  
                  }
  
                  if ( false === $show_category ) {
                      $exclude[] = $category-&gt;term_id;
                  }
  
              }
  
              if ( ! empty( $exclude ) ) {
                  $GLOBALS['cat_exclude'] = implode( ',', $exclude );
              }
          //}
      }
  
      add_action('wp_head', 'getExcludedCats');
  </code></pre>
  
  <p>However, in the shop view itself, the category is still visible. How can I remove it there, when no items in this category are visible to the user.</p>
  
  <p>I have tried:
  <a href="https://gist.github.com/rynaldos/a9d357b1e3791afd9bea48833ff95994" rel="nofollow noreferrer">https://gist.github.com/rynaldos/a9d357b1e3791afd9bea48833ff95994</a>
  But it removes the category ALWAYS, in both widget and shop.</p>
  
  <p>Products are being displayed by group membership of the customer:
  <a href="https://wordpress.org/plugins/groups/" rel="nofollow noreferrer">https://wordpress.org/plugins/groups/</a></p>