I need to get category list with sub categories. I've found multiple solutions that I may need to create a module and get category list inside module's template file. But I need to do this without creating a module. How can I do it?

I tried creating a class and I used those included magento classes in the answers I mentioned with use inside my phtml file, before declaring my class. but it didn't work.

Is it possible to get every category with its sub categories (and if sub category has its own sub category, I need to get it too ) without creating a module? if so, how can I do it? (I need to do this inside single phtml file)


Please check this..

$categories = $block->getStoreCategories();
foreach ($categories as $category) {
    echo $category->getName();

    $subCategories = $block->getCategory($category->getId());
    foreach ($subCategories as $subCategory) {
        echo $subCategory->getName();
  • This causes an error. $categories = $block->getStoreCategories(); – Joey Nov 1 '17 at 5:56
  • Please use this inside Magento_Catalog/templates/product/... any file... – Arslan Tabassum Aug 18 '18 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.