Support » Plugin: WooCommerce » Display products from category but not subcategory

  • Resolved pinksharpii

    (@pinksharpii)


    I am using a shortcode to display products from a certain category. But I need it to display only the first level products.

    Here is my category structure:
    – Transformers
    — Transformer Accessories

    I need to only display Transformers not, Accessories, which is the subcat.
    But this shortcode will display all from the transformers category:

    [products category="transformers"]

    And if I change it to be NOT IN Transformer Accessories, it displays all other products in other categories outside of Transformers as well.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support mother.of.code

    (@imazed)

    The Mother of Code | Automattic Happiness Engineer

    You’re going to need a bit of custom code here – the shortcodes included in the plugin do not cover the kind of excluding you’re looking for. This article by SkyVerge looks like a good starting point for you: https://www.skyverge.com/blog/hide-certain-woocommerce-subcategories-catalog/

    That doesn’t work. That is for hiding categories in a list of categories. I’m needing to hide products from a certain category in a list of products.

    I’ve tried all of the following based on forum responses. None change the results.

    function modify_category_products($query)
        {
            if($query->get('product_cat') && is_tax() && !is_admin())
            {
                $tax_query = $query->get('tax_query');
    
                $cat = get_term_by('slug', $query->get('product_cat'), 'product_cat');
                $subcats = get_terms( 'product_cat', array(
                    'parent' => $cat->term_id
                ) );
    
                $child_cats = array();
                foreach($subcats as $cat)
                {
                    array_push($child_cats, $cat->term_id);
                }
    
                $new_tax_query = array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'id',
                        'terms' => $child_cats,
                        'operator'=> 'NOT IN'
                    )
                );
                array_push($tax_query, $new_tax_query);
                $query->set('tax_query', $new_tax_query);
            }
        }
        add_filter( 'pre_get_posts', 'modify_category_products' );

    and

    function modify_category_products($query)
        {
            if($query->get('product_cat') && is_tax() && !is_admin())
            {
                $tax_query = $query->get('tax_query');
    
                $cat = get_term_by('slug', $query->get('product_cat'), 'product_cat');
                $subcats = get_terms( 'product_cat', array(
                    'parent' => $cat->term_id
                ) );
    
                $child_cats = array();
                foreach($subcats as $cat)
                {
                    array_push($child_cats, $cat->term_id);
                }
    
                $new_tax_query = array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'id',
                        'terms' => $child_cats,
                        'operator'=> 'NOT IN'
                    )
                );
                array_push($tax_query, $new_tax_query);
                
                $query->tax_query->queries[] = $new_tax_query; 
                $query->query_vars['tax_query'] = $query->new_tax_query->queries;
            }
        }
        add_filter( 'pre_get_posts', 'modify_category_products' );

    and

    function modify_category_products_interrupt($query)
        {
            if(is_main_query() && !is_admin())
            {
                $queries = $query->tax_query->queries;
                foreach($queries as $q)
                {
                    if($q['taxonomy'] == 'product_cat')
                    {
                        $q['include_children'] = false;
                    }
                }
    
            }
        }
    add_action('parse_tax_query', 'modify_category_products');
    Zandy Ring

    (@zandyring)

    Automattic Happiness Engineer

    This discussion is a bit old, but it may help – specifically the suggestion to not use cat, as it references all subcategories of a category:
    https://wordpress.stackexchange.com/questions/136486/exclude-sub-category-posts-from-category-display

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display products from category but not subcategory’ is closed to new replies.