Magento2 Get List of Categories

In this blog post, we will see how to get the list of all the categories in Magento2 using object manager.

Please find the below code snippet to achieve this.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

// get list of all the categories
$categories = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory')->create();
$categories->addAttributeToSelect('*');

foreach ($categories as $category) {
echo 'Catgeory Id: '.$category->getId();
echo 'Catgeory Name: '.$category->getName();
echo 'Catgeory Url: '.$category->getUrl();
}

// Get current store categories
$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categories = $categoryHelper->getStoreCategories();

foreach ($categories as $category) {
echo 'Catgeory Id: '.$category->getId();
echo 'Catgeory Name: '.$category->getName();
}

Hope this helps you guys!

Leave a Reply