WooCommerce: Show nesting of categories in product filter

Hi there,

I’m trying to find a way to show parent/child categories in either the text list or the dropdown of the Product filter element but neither seems to support them? It’s simply showing them alphabetically. That’s a bit of a mess tbh. Does anyone have an idea how to do this?

image
(More Testing is a Child of Testing)

On a side note: The products element should be able to show the categories and subcategories the same way woocommerce does on its loop. I know there’s a tutorial out there now for that but it does not work with subcategories either.

Thanks in advance for any help with the above part.

1 Like

So, I ended up displaying a styled list of top categories above the products on the main shop page and added a shortcode to the archive pages displaying subcategories if there are any.

This is the code:

/*
 * The shortcode is [sub_level_product_categories_list] 
 */
add_shortcode('sub_level_product_categories_list', 'wc_shortcode_sub_level_product_categories_list'); 
function wc_shortcode_sub_level_product_categories_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'      => 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="subcategories-list-item"><a href="'. $term_link .'">'. $term->name .'</a></li>';
    }

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