Home > Net >  How to show only current parent product category with subcategories and hide others in widget on the
How to show only current parent product category with subcategories and hide others in widget on the

Time:06-25

For example, there are 2 different catalogs - men and women (clothes).

There is a widget in sidebar.

Is it possible to hide 'men' category with its child subcategories in all 'women' category pages and subcategory pages, and the same in 'men' catalog in all parent categories and child subcategories hide all from 'women'? From product cat widget.

I tried conditions like

if( has_term(267, 'product_cat' ) ) {
    add_filter( 'woocommerce_product_categories_widget_args', 'organicweb_exclude_widget_category' );
    function organicweb_exclude_widget_category( $args ) {
    // Enter the id of the category you want to exclude in place of '30'
            $args['exclude'] = array('262' );
            return $args;
    }
} 


if( is_product_category( array( '267',) ) ) {
    add_filter( 'woocommerce_product_categories_widget_args', 'organicweb_exclude_widget_category' );
    function organicweb_exclude_widget_category( $args ) {
    // Enter the id of the category you want to exclude in place of '30'
            $args['exclude'] = array('262' );
            return $args;
    }
} 

The filter works without 'if///{}', hides excluded category from widget at all, but not with if. I start to think that the simplest way is to hide them using css(body class parent li id), but maybe it is a correct way?

CodePudding user response:

it's good to hide it for the user using CSSS than hiding it permanently with code.

CodePudding user response:

You can solve it like this way:

$args = array(
    'post_type'      => 'product',
    'posts_per_page' => 10,
    'product_cat'    => 'your-cat-name'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    global $product;
    echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
endwhile;
wp_reset_query(); 

Also this article will help you https://www.codecheef.org/article/how-to-display-category-wise-product-in-woocommerce

Thank you

  • Related