How to display all the subcategories from a specific category in Woocommerce?

In this tutorial we’ll learn how to display all the subcategories of a specific category. To achieve this first you need to create a few categories. To create a new category go to categories section under products tab and add some parent and child categories.

So, for the example purpose I’ve created a parent category and 2 child categories of that specific parent.

Now we need to display these categories. You can display these categories wherever you want in the theme. We can display child categories of a parent category either by parent category id or by parent category name. Add following lines of code in your functions.php file so that we can call them in any of the template/theme file.

function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID) {

   $args = array(

       'hierarchical' => 1,

       'show_option_none' => '',

       'hide_empty' => 0,

       'parent' => $parent_cat_ID,

     'taxonomy' => 'product_cat'

   );

$subcats = get_categories($args);

echo '<ul class="wooc_sclist">';

foreach ($subcats as $sc) {

       $link = get_term_link( $sc->slug, $sc->taxonomy );

echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';

     }

echo '</ul>';

}

function woocommerce_subcats_from_parentcat_by_NAME($parent_cat_NAME) {

$IDbyNAME = get_term_by('name', $parent_cat_NAME, 'product_cat');

$product_cat_ID = $IDbyNAME->term_id;

   $args = array(

       'hierarchical' => 1,

       'show_option_none' => '',

       'hide_empty' => 0,

       'parent' => $product_cat_ID,

       'taxonomy' => 'product_cat'

   );

$subcats = get_categories($args);

echo '<ul class="wooc_sclist">';

foreach ($subcats as $sc) {

       $link = get_term_link( $sc->slug, $sc->taxonomy );

echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';

     }

echo '</ul>';

}

To access functions.php file go to editor section under appearance and look for functions.php in right column.

Now we are all set to display our subcategories we just need to call any of these functions.

So let’s say I want to display these subcategories in the product archive page i.e. shop page.

So go to plugins section and click edit under Woocommerce plugin. Look for Woocommerce/templates/archive-product.php in the right column and call the above function where you want to display.

For example purpose I will display subcategories at top of this page. Add the following line of code in this file.

woocommerce_subcats_from_parentcat_by_ID(6);

Here ‘6’ is the parent category id. To find the category id of your parent category id, go to category page and click to edit the category. You’ll see id in the url.

Enter your parent id in the function. Refresh the page and your subcategories will be displayed.

You can display sub-categories by parent category name as well. For that you need to call following function and pass your parent category name as parameter.

woocommerce_subcats_from_parentcat_by_NAME('Parent Category');

Add the Code like I did above.

Editorial Staff

ThemeLocation Provides Wordpress & Woocommerce development & Customization services. Have a Project? Contact To discuss

2 thoughts on “How to display all the subcategories from a specific category in Woocommerce?

  1. How would this function be customised to show the following format:

    PARENT
    * Child Parent
    * Child
    * Child Parent
    * Child
    * Child Parent
    * Child

    Any help with this would be great currently it shows the current parent and a child, I need it to show one more level down.

    Thanks

    1. Here is the Code:

      function woocommerce_subcats_from_parentcat_by_NAME($parent_cat_NAME) {

      $IDbyNAME = get_term_by(‘name’, $parent_cat_NAME, ‘product_cat’);

      $product_cat_ID = $IDbyNAME->term_id;

      $args = array(

      ‘hierarchical’ => 1,

      ‘show_option_none’ => ”,

      ‘hide_empty’ => 0,

      ‘parent’ => $product_cat_ID,

      ‘taxonomy’ => ‘product_cat’

      );

      $parent_link = get_term_link( $IDbyNAME->slug, $IDbyNAME->taxonomy );
      echo ‘‘.$IDbyNAME->name.’‘;
      $subcats = get_categories($args);

      echo ‘

        ‘;

        foreach ($subcats as $sc) {

        $link = get_term_link( $sc->slug, $sc->taxonomy );

        echo ‘

      • ‘.$sc->name.’
      • ‘;

        $childIdByName = get_term_by(‘name’, $sc->name, ‘product_cat’);
        $parent_cat_ID = $childIdByName->term_id;

        $args1 = array(
        ‘hierarchical’ => 1,
        ‘show_option_none’ => ”,
        ‘hide_empty’ => 0,
        ‘parent’ => $parent_cat_ID,
        ‘taxonomy’ => ‘product_cat’
        );

        echo ‘

        ‘;
        $subcats1 = get_categories($args1);
        foreach ($subcats1 as $sc1) {
        $link1 = get_term_link( $sc1->slug, $sc1->taxonomy );
        echo ‘

      • ‘.$sc1->name.’
      • ‘;
        }

        echo ‘

      ‘;

      }

      echo ‘‘;
      }

Leave a Reply

Your email address will not be published. Required fields are marked *