Support » Plugin: WooCommerce » hide products at specific adult category page

  • Resolved stylebutton

    (@stylebutton)


    Hi,

    i want to hide the products at a specific category page.

    Background: I have an adult category with hidden sub categories (via functions.php). Because of the hidden sub categories, it shows the products, despite woocommerce should show only sub categories.

    Is it possible to hide products only at a specific category page? But it is compellingly, that the sub category page shows the products!

    Thanks a lot!
    Holger

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Phillip C.

    (@phillipwoo)

    Hello,

    It sounds like this support document might help:

    https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/

    Please let us know how this works for you.

    Thanks!

    Thanks for this hint. But unfortunately, this also hide the products at the subcategories pages.

    The solution was to include a condition. First is for hiding the sub categories. Second to remove products only at specific category page.

    /** removes Sub categories **/
    add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
    
    function get_subcategory_terms( $terms, $taxonomies, $args ) {
    
      $new_terms = array();
    
      // if a product category and on the shop page
      if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_product_category( 'buttons' ) ) {
    
        foreach ( $terms as $key => $term ) {
    
          if ( ! in_array( $term->slug, array( 'slug' ) ) ) { // slug -> Sub categories to hide
            $new_terms[] = $term;
          }
    
        }
    
        $terms = $new_terms;
      }
    
      return $terms;
    }
    /**
    
     * Exclude products from a particular category on the shop page, but not from sub categories
    
    */
    add_action( 'woocommerce_product_query', 'hideproduct_pre_get_posts_query' );  
    
    function hideproduct_pre_get_posts_query( $q ) {
    
     if (!is_admin() && is_product_category( 'slug' ) && is_main_query()) { // hide only at category slug
    
        $tax_query = (array) $q->get( 'tax_query' );
        $tax_query[] = array(
               'taxonomy' => 'product_cat',
               'field' => 'slug',
               'terms' => array( 'slug' ), // Don't display products from this category
               'operator' => 'NOT IN'
        );
     }
        $q->set( 'tax_query', $tax_query );
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘hide products at specific adult category page’ is closed to new replies.