The functionality I need to implement for a Woocommerce store is this:
On a product category’s archive page, I need to have a dropdown that displays that category’s subcategories. Once a subcategory is selected, I need a product table to filter based on that subcategory. Then I need another dropdown to appear/enable to select subcategories of the subcategory selected, and that whole process will be repeated until no subcategories remain (likely 3-4 levels deep max).
Is there a plugin that can do this?
If not, I kind of already tried taking a stab at implementing this myself, but I’m unsure of how to continue from the point I’m at now.
I made a function that recursively gets all of a category’s subcategories and their subcategories and so on. It takes in a term\_id as it’s first argument and returns an array.
function get_categories_list( $parent, $root_parent = null) {
if ($root_parent) {
$_parent = $root_parent;
} else {
$_parent = $parent;
}
$args = array(
‘taxonomy’ => ‘product_cat’,
‘parent’ => $parent
);
$categories = get_categories($args);
$result = array();
if ($categories) {
foreach ($categories as $cat) {
if (term_is_ancestor_of( $_parent, $cat->cat_ID, ‘product_cat’ )) {
array_push($result, array(
‘cat’ => $cat->name,
‘subcats’ => get_categories_list( $cat->cat_ID, $_parent )
));
}
}
}
return $result;
}
How could I populate the dropdowns with subcategories, and how could I then change the query to filter the product table, preferably asynchronously?