1

For now I'm getting category name.

This is my code:

app/code/Vendor/Module/view/frontend/templates/storecategories.phtml

<?php
        $categoryId = 65;
        $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $category = $_objectManager->create('Magento\Catalog\Model\Category')
            ->load($categoryId);
        $parent = $category->getName();
        echo $parent?>

What would be next steps to get current categories subcategories?

2 Answers 2

3

Try This

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
 $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');// Instance of Category Model
 $categoryId = 15; // YOUR CATEGORY ID
 $category = $categoryFactory->create()->load($categoryId);
 // Children  Categories
 $childrenCategories = $category->getChildrenCategories();
5
  • When I try to echo $category->getChildrenCategories(); I got this error: Recoverable Error: Object of class Magento\Catalog\Model\ResourceModel\Category\Collection could not be converted to string Maybe you know the solution?
    – Rufus
    Feb 19, 2019 at 14:52
  • $ChildrenCategories is in form of array try printing it by print_r(childrenCategories)
    – Ghulam.M
    Feb 19, 2019 at 14:59
  • I intend to put it into foreach loop and display it in <ul> <li>. I suppose it's not possible with print_r. Do you have any suggestion?
    – Rufus
    Feb 19, 2019 at 15:08
  • try using for each loop foreach($childrenCategories as $childSubcategorie) { echo ' --> '.$childSubcategorie->getName().'<br/>'; }
    – Ghulam.M
    Feb 19, 2019 at 15:29
  • Thank's a lot!!
    – Rufus
    Feb 19, 2019 at 15:33
0

Have you tried the following?

 $category->getChildrenCategories();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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