How to display only child categories and products in woocommerce?

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

How to display only child categories and products in woocommerce?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cortez.connelly , a year 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.

Related Threads:

How to display pagination for woocommerce products?
How to resize the display size of woocommerce products?
How to display only products in shopify search results?
How to customize the categories page in woocommerce?
How to update price of all products in woocommerce?
How to add variable tax on products in woocommerce?