Loading subcategories for a category in Magento 2
If you don;t know how to load subcategories for a particular category in Magento 2, you’ve come to the right place. Below, we will explain this procedure.
Dmitry Fedyuk on Mage2.PRO recommends to utilize the
1 2 3 4 5 6 7 8 9 |
/** * Return children categories of current category * * @return \Magento\Catalog\Model\ResourceModel\Category\Collection|\Magento\Catalog\Model\Category[] */ public function getChildrenCategories() { return $this->getResource()->getChildrenCategories($this); } |
This is how your code should look like on practice:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
return $this->_currentCategoryKey; } /** * Retrieve child categories of current category * * @return \Magento\Framework\Data\Tree\Node\Collection */ public function getCurrentChildCategories() { $categories = $this->_catalogLayer->getCurrentCategory()->getChildrenCategories(); /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $this->_productCollectionFactory->create(); $this->_catalogLayer->prepareProductCollection($productCollection); $productCollection->addCountToCategories($categories); return $categories; } /** * Checkin activity of category * |