Get the subcategories of the current product category in Woocommerce archives

11,773

The following code will display the formatted linked product subcategories from current product category for product category archive pages:

if ( is_product_category() ) {

    $term_id  = get_queried_object_id();
    $taxonomy = 'product_cat';

    // Get subcategories of the current category
    $terms    = get_terms([
        'taxonomy'    => $taxonomy,
        'hide_empty'  => true,
        'parent'      => get_queried_object_id()
    ]);

    $output = '<ul class="subcategories-list">';

    // Loop through product subcategories WP_Term Objects
    foreach ( $terms as $term ) {
        $term_link = get_term_link( $term, $taxonomy );

        $output .= '<li class="'. $term->slug .'"><a href="'. $term_link .'">'. $term->name .'</a></li>';
    }

    echo $output . '</ul>';
}

Tested and works.


USAGE EXAMPLES:

1) You can use this code directly in archive-product.php template file.

2) You can embed the code in a function, replacing the last line echo $output . '</ul>'; by return $output . '</ul>';, as for shortcodes, the display is always returned.

3) You can embed the code using action hooks like woocommerce_archive_description:

// Displaying the subcategories after category title
add_action('woocommerce_archive_description', 'display_subcategories_list', 5 ); 
function display_subcategories_list() {
    if ( is_product_category() ) {

        $term_id  = get_queried_object_id();
        $taxonomy = 'product_cat';

        // Get subcategories of the current category
        $terms    = get_terms([
            'taxonomy'    => $taxonomy,
            'hide_empty'  => true,
            'parent'      => $term_id
        ]);

        echo '<ul class="subcategories-list">';

        // Loop through product subcategories WP_Term Objects
        foreach ( $terms as $term ) {
            $term_link = get_term_link( $term, $taxonomy );

            echo '<li class="'. $term->slug .'"><a href="'. $term_link .'">'. $term->name .'</a></li>';
        }

        echo '</ul>';
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


To display it after the category description, change the hook priority from 5 to 20 in:

add_action('woocommerce_archive_description', 'display_subcategories_list', 5 ); 

like:

add_action('woocommerce_archive_description', 'display_subcategories_list', 20 ); 
Share:
11,773

Related videos on Youtube

Alvaro
Author by

Alvaro

Updated on June 04, 2022

Comments

  • Alvaro
    Alvaro almost 2 years

    I'm trying to Show subcategories (not subsubcategories,etc) under current category in Woocommerce like this Web: http://www.qs-adhesivos.es/app/productos/productos.asp?idioma=en

    For Example, Construction is the category, and Sealants & adhesives, waterproofing, plyurethane foams… are subcategories.

    Sealants & Mastics is the category, and ACETIC SILICONE SEALANT, NEUTRAL SILICONE SEALANT, ACRYLIC SEALANT… are subcategories…

    Already have an archive-product.php in a woocommerce folder under my child theme.

    Already tried some code and it applies but it's not what I want.

  • Alvaro
    Alvaro over 4 years
    And where I need to put it? in function.php?
  • Alvaro
    Alvaro over 4 years
    And what about: } elseif (!woocommerce_product_subcategories(array('before' => woocommerce_product_loop_start(false), 'after' => woocommerce_product_loop_end(false)))) { in archive-product.php ?
  • Alvaro
    Alvaro over 4 years
    Hi, first of all, many thanks for your answers and help. But is not working what I want. I should explain best. See OP again.
  • Alvaro
    Alvaro over 4 years
    Yes, you are correct. I made a mistake explaining what I want. Should I open new question and let this as before? Thanks again!
  • Hannah James
    Hannah James over 2 years
    Thanks for the perfect code.