1

How can I get a list of all category names and their ID's from a store on Magento 2.2?

For example something like this

  • Clothes 2
  • Shirts 5
  • T-shirts 7
  • T-shirts 8
  • Shoes 10
  • Sport shoes 12
  • ...

It can be in any format and displayed anywhere on the site as long as I can copy it as text. I will disable this functionality after, so I don't mind if it's a quick and dirty way as it's never going on the live site.

Basically I'm trying to find a way to get all product names and ID's without having to individually click through every item in the Magento category tree.

  • do you want category name and id, right? – Dhiren Vasoya May 15 at 10:45
  • Yes that's correct. A list of ALL category names and ID's from the store in one list. – Jarxberg May 15 at 10:47
  • check answer I post. – Dhiren Vasoya May 15 at 10:55
  • you got your answer or not? – Dhiren Vasoya May 15 at 12:04
0

Using Object Manager

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
$categoryCollection = $objectManager-
  >get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
 $categories = $categoryCollection->create();
 $categories->addAttributeToSelect('*');

   foreach ($categories as $category) {

      echo $category->getName() . '<br />';
   }

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

  foreach ($categories as $category) {    
   echo $category->getName() . '<br />';
   echo $category->getId() . '<br />';
} 
  • Thanks Divya, I got this to work with some editing. 1. On line 2 there shouldn't be space between $objectManager- and >get 2. The first foreach only lists category names, not ID's. I copied the ID line from lower down on your code. 3. The second half of the code is not needed. – Jarxberg May 15 at 13:44
  • This is the edited version that worked for me: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory'); $categories = $categoryCollection->create(); $categories->addAttributeToSelect('*'); foreach ($categories as $category) { echo $category->getName() . ' / '; echo $category->getId() . '<br />'; } – Jarxberg May 15 at 13:51
  • Thnx for response – Divya May 16 at 8:56
0

First, you need to create the block at the following location:

Vendor\Extension\Block\Getcategoryinfo.php

<?php
namespace Vendor\Extension\Block;

class Getcategoryinfo extends \Magento\Framework\View\Element\Template
{ 
    protected $_categoryCollectionFactory;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
        array $data = []
    ) {
        $this->_categoryCollectionFactory = $categoryCollectionFactory;
        parent::__construct($context, $data);
    }

    public function getCategoryCollection() {
        $collection = $this->_categoryCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addIsActiveFilter(); 
        return $collection;
    }
}

Now call that block function into .phtml file like this:

// get the list of all categories
$categories = $block->getCategoryCollection(); 
foreach ($categories as $category) {
    echo $category->getName() .' '.$category->getId() . '<br />'; 
}
  • Not sure if I did something wrong, but I'm getting the following error on front end: 'Exception #0 (Exception): Warning: Invalid argument supplied for foreach() in (path to my phtml file)' – Jarxberg May 15 at 13:17
  • Have you create custom block into your extension? And put the below code into your respective phtml file? – Dhiren Vasoya May 15 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.