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() . '
';
}

That’s it!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s