How to display only child categories and products in woocommerce?

by hal.littel , in category: PHP CMS , a month ago

How to display only child categories and products in woocommerce?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cortez.connelly , a month ago

@hal.littel 

To display only child categories and products in WooCommerce, you can use the following code snippet in your theme's functions.php file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function custom_product_hierarchy( $args ) {
    $args['hide_empty'] = 0;
    $args['parent'] = 0;
    return $args;
}
add_filter( 'woocommerce_product_categories_widget_args', 'custom_product_hierarchy' );

function custom_product_query( $q ) {
    $q->set( 'post_parent', 0 );
    return $q;
}
add_filter( 'woocommerce_product_query', 'custom_product_query' );


This code snippet filters the product categories widget to only display child categories and sets the parent category to 0 when querying products, effectively displaying only child categories and products.