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?
asked Feb 19, 2019 at 14:05
Rufus
6311 gold badge7 silver badges22 bronze badges
2 Answers 2
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();
-
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– Rufus2019年02月19日 14:52:03 +00:00Commented Feb 19, 2019 at 14:52
-
$ChildrenCategories is in form of array try printing it by print_r(childrenCategories)Ghulam.M– Ghulam.M2019年02月19日 14:59:52 +00:00Commented 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– Rufus2019年02月19日 15:08:44 +00:00Commented Feb 19, 2019 at 15:08
-
try using for each loop foreach($childrenCategories as $childSubcategorie) { echo ' --> '.$childSubcategorie->getName().'<br/>'; }Ghulam.M– Ghulam.M2019年02月19日 15:29:21 +00:00Commented Feb 19, 2019 at 15:29
-
Have you tried the following?
$category->getChildrenCategories();
Explore related questions
See similar questions with these tags.
default