Products Nested by Subcategories in a current category archive

I am able to get a list of its subcategories in a category archive page using below code in archive-product.php but I want to display the list of the products currently assigned to each subcategory.

<?php
$parentid = get_queried_object_id();
$args = array(
‘parent’ => $parentid
);
$categories = get_terms(
‘product_cat’, $args
);
if ( $categories ) {
foreach ( $categories as $category ) {
echo $category->name;
}
}
?>

Example:

Subcategory 1

Product 1 of Subcategory 1
Product 2 of Subcategory 1

Subcategory 2

Product 1 of Subcategory 2
Product 2 of Subcategory 2

I am still learning WordPress so any help will be highly appreciated.

Read more here:: Products Nested by Subcategories in a current category archive

Leave a Reply

Your email address will not be published. Required fields are marked *