How to Display Only Child Categories And Products In Woocommerce?

5 minutes read

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
add_action( 'woocommerce_product_subcategories_args', 'only_child_categories' );
function only_child_categories( $args ) {
    $args['parent_category'] = 0; // Change this to the parent category ID you want to display children from
    $args['terms'] = get_terms( 'product_cat', array( 'parent' => $args['parent_category'] ) );
    return $args;
}

add_filter( 'woocommerce_shortcode_products_query', 'only_child_products', 10, 3 );
function only_child_products( $query_args, $atts, $loop_name ) {
    $query_args['tax_query'] = array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => get_terms( 'product_cat', array( 'parent' => $query_args['category'] ) ),
        ),
    );
    return $query_args;
}


This code will modify the WooCommerce product subcategories to only display child categories of a specified parent category, as well as filter the products to only show those within the child categories. Make sure to replace the parent_category variable with the ID of the desired parent category.

Bet WordPress Hosting Providers of November 2024

1
AWS

Rating is 5 out of 5

AWS

2
DigitalOcean

Rating is 4.9 out of 5

DigitalOcean

3
Vultr

Rating is 4.8 out of 5

Vultr

4
Cloudways

Rating is 4.6 out of 5

Cloudways


How to exclude parent categories from WooCommerce category pages?

To exclude parent categories from WooCommerce category pages, you can use the following steps:

  1. Go to your WordPress dashboard and navigate to the "Appearance" tab.
  2. Click on "Theme Editor" and locate the "functions.php" file in your theme editor.
  3. Add the following code snippet at the end of the functions.php file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
function exclude_parent_category( $query ) {
if ( $query->is_tax('product_cat') && $query->is_main_query() ) {
    $term_id = get_queried_object_id();
    $term = get_term( $term_id, 'product_cat' );
    if ( $term->parent != 0 ) { 
        $query->set( 'term_id', $term_id );
    } else {
        $query->set( 'tax_query', array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'term_id',
                'terms' => $term_id,
                'operator' => 'IN',
                'include_children' => true,
            )
        ) );
    }
} return $query; }

add_filter( 'pre_get_posts', 'exclude_parent_category' );


  1. Save the changes to the functions.php file.


This code snippet will exclude parent categories from the WooCommerce category pages and only display child categories and products.


How to only show child products in WooCommerce?

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

1
2
3
4
5
6
function modify_shop_query($q) {
    if (is_shop()) {
        $q->set('post_parent', 0);
    }
}
add_action('pre_get_posts', 'modify_shop_query');


This code snippet will modify the main shop query to only display products that do not have a parent product, which essentially means displaying only child products. Adding this code to your functions.php file will ensure that only child products are shown on the shop page in WooCommerce.


How to limit category display to child categories in WooCommerce?

To limit category display to only child categories 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
add_filter( 'get_terms_args', 'limit_categories_to_children', 10, 2 );

function limit_categories_to_children( $args, $taxonomies ) {
    if ( $taxonomies[0] == 'product_cat' ) {
        if ( isset( $args['parent'] ) && $args['parent'] == 0 ) {
            $args['parent'] = false;
            $args['child_of'] = get_queried_object_id();
        }
    }

    return $args;
}


This code snippet hooks into the get_terms_args filter to modify the arguments used in the get_terms() function, specifically for the product_cat taxonomy. It checks if the parent argument is set to 0 (top level categories) and if so, it replaces it with the ID of the current category being viewed (get_queried_object_id()).


This will ensure that only child categories of the current category will be displayed in WooCommerce category archives or on the shop page.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To update the price of all products in WooCommerce, you can use the bulk editing feature available in the Products section of your WooCommerce dashboard. Simply navigate to Products > All Products, select all the products you'd like to update, and click...
In WordPress, adding and managing categories is a fundamental aspect of organizing and categorizing your website's content. Categories help in structuring your content in a logical manner, making it easier for users to navigate and find relevant informatio...
To get all the parents of a child XML node in PowerShell, you can use the ParentNode property of the XML node object. You can start by selecting the child node and then recursively navigating through the parent nodes until you reach the top-level parent node. ...