Magento 2: Get list of all Categories & Store Categories
This article shows how we can get list of all categories and all category of current store in Magento 2.
Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory and \Magento\Catalog\Helper\Category classes in the constructor of my module’s block class.
app/code/Chapagain/HelloWorld/Block/HelloWorld.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?php namespace Chapagain\HelloWorld\Block; class HelloWorld extends \Magento\Framework\View\Element\Template { protected $_categoryCollectionFactory; protected $_categoryHelper; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory, \Magento\Catalog\Helper\Category $categoryHelper, array $data = [] ) { $this->_categoryCollectionFactory = $categoryCollectionFactory; $this->_categoryHelper = $categoryHelper; parent::__construct($context, $data); } /** * Get category collection * * @param bool $isActive * @param bool|int $level * @param bool|string $sortBy * @param bool|int $pageSize * @return \Magento\Catalog\Model\ResourceModel\Category\Collection or array */ public function getCategoryCollection($isActive = true, $level = false, $sortBy = false, $pageSize = false) { $collection = $this->_categoryCollectionFactory->create(); $collection->addAttributeToSelect('*'); // select only active categories if ($isActive) { $collection->addIsActiveFilter(); } // select categories of certain level if ($level) { $collection->addLevelFilter($level); } // sort categories by some value if ($sortBy) { $collection->addOrderField($sortBy); } // select certain number of categories if ($pageSize) { $collection->setPageSize($pageSize); } return $collection; } /** * Retrieve current store categories * * @param bool|string $sorted * @param bool $asCollection * @param bool $toLoad * @return \Magento\Framework\Data\Tree\Node\Collection or * \Magento\Catalog\Model\ResourceModel\Category\Collection or array */ public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true) { return $this->_categoryHelper->getStoreCategories($sorted = false, $asCollection = false, $toLoad = true); } } ?> |
Now, we fetch and print the category collection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $categories = $block->getCategoryCollection(); foreach ($categories as $category) { //print_r($category->getData()); echo $category->getName() . '<br />'; } // get categories sorted by category 'name' $categories = $block->getCategoryCollection(true, false, 'name', false); foreach ($categories as $category) { echo $category->getName() . '<br />'; } // get current store's categories $categories = $block->getStoreCategories(); foreach ($categories as $category) { echo $category->getName() . '<br />'; } |
Using Object Manager
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $appState = $objectManager->get('\Magento\Framework\App\State'); $appState->setAreaCode('frontend'); $categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory'); $categories = $categoryCollection->create(); $categories->addAttributeToSelect('*'); foreach ($categories as $category) { //print_r($category->getData()); echo $category->getName() . '<br />'; } $categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category'); $categories = $categoryHelper->getStoreCategories(); foreach ($categories as $category) { echo $category->getName() . '<br />'; } |
Hope this helps. Thanks.





Mukesh Chapagain is a graduate of Kathmandu University (Dhulikhel, Nepal) from where he holds a Masters degree in Computer Engineering. Mukesh is a passionate web developer who has keen interest in open source technologies, programming & blogging.