phpwordpresswoocommercehierarchicaltaxonomy-terms

Display the sub-subcategories of the current subcategories in Woocommerce


I'm trying to Show sub-subcategories under current subcategory in Woocommerce like this Website.

I've 2 parents categories "Products" and "Sectors". Then I've a menu link that go to both.

When I'm in "Products" I want to see the picture of the subcategory, the title of category and then all the sub-subcategories with the title and link to them.

For Example, the parent category is "Products", Construction is the subcategory, and Sealants & adhesives, waterproofing, plyurethane foams… are sub-subcategories.

Sealants & Mastics is the subcategory, and ACETIC SILICONE SEALANT, NEUTRAL SILICONE SEALANT, ACRYLIC SEALANT… are sub-subcategories…

Here is a screenshot that explains it better:

enter image description here


Solution

  • The code to be used in here is very similar to your previous question thread. But we use a specific action hook with some few changes to get the sub-subcategories of the subcategories:

    // Displaying the sub-subcategories of the current subategories
    add_action('woocommerce_after_subcategory', 'display_subsubcategories_list', 20, 1 );
    function display_subsubcategories_list( $category ) {
        $taxonomy = 'product_cat';
    
        // Get sub-subcategories of the current subcategory
        $terms    = get_terms([
            'taxonomy'    => $taxonomy,
            'hide_empty'  => true,
            'parent'      => $category->term_id
        ]);
    
        if( count($terms) > 0 ) :
    
        echo '<ul class="subcategories-list" style="list-style: none; border: solid 1px #ddd; border-bottom: none;">';
    
        // Loop through product sub-subcategories WP_Term Objects
        foreach ( $terms as $term ) {
            $term_link = get_term_link( $term, $taxonomy );
    
            echo '<li class="'. $term->slug .'" style="border-bottom: solid 1px #ddd;"><a href="'. $term_link .'">'. $term->name .'</a></li>';
        }
    
        echo '</ul>';
    
        endif;
    }
    

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

    Notes: The woocommerce_after_subcategory action hook is located on content-product_cat.php template file, which handle subcategories to be displayed as products are (with an image and a linked term name).
    For that your main categories needs to have the option "Display type" set on "Subcategory".