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?

Amit Bera
77.8k21 gold badges127 silver badges240 bronze badges
asked Feb 19, 2019 at 14:05

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();
answered Feb 19, 2019 at 14:16
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? Commented Feb 19, 2019 at 14:52
  • $ChildrenCategories is in form of array try printing it by print_r(childrenCategories) Commented 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? Commented Feb 19, 2019 at 15:08
  • try using for each loop foreach($childrenCategories as $childSubcategorie) { echo ' --> '.$childSubcategorie->getName().'<br/>'; } Commented Feb 19, 2019 at 15:29
  • Thank's a lot!! Commented Feb 19, 2019 at 15:33
0

Have you tried the following?

 $category->getChildrenCategories();
answered Feb 19, 2019 at 14:11
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.