I added the following to my WordPress theme functions.php file, to show product subcategories inside the WooCommerce product category archive page, and it works well:
add_action('woocommerce_before_shop_loop', 'dcms_show_subcategories');
function dcms_show_subcategories(){
if ( is_product_category() ){
$parentid = get_queried_object_id();
$cat_args = [ 'parent' => $parentid,
'hide_empty' => false ];
$subcategories = get_terms('product_cat', $cat_args);
if ( count($subcategories ) ){
echo "<ul class='dcms-subcategories'>";
foreach ($subcategories as $subcategory) {
$link = get_category_link($subcategory->term_id);
$name = $subcategory->name;
echo "<li><a href='{$link}'>{$name}</a></li>";
}
echo "</ul>";
}
}
}
Now, I need to show the product categories inside the subcategory product page, but I can't resolve it. Can anyone help me, please?
I searched online, but I didn't find any solution.
Everything can fit in a unique function that will display first parent terms and then child terms, replacing your current code:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.