how to get parent category collection dependency injection magento 2.3.1

Magento Asked by Ravindrasinh Zala on August 3, 2020

How can I get a first category level from Magento 2.3, my category setup looks like this now.

    |-First  |-Shoes

Now First category click open sub category like shoes and T-Shirt in magento 2.3 dependency injection

3 Answers

you can get the whole category path form this code and can separate the level you want even if you product have only current category it will gives you full path with name of categories: |

$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$category_Ids = $block->getProduct()->getCategoryIds(); /*will return category ids array*/
$index = count($category_Ids)-1;
$lastcatid = $category_Ids[$index];
$categoryFactory = $objectManager->get('MagentoCatalogModelCategoryFactory');// Instance of Category Model // YOUR CATEGORY ID
$category = $categoryFactory->create()->load(intval($lastcatid));
// Parent Categories
 $categoryall ="";
 $parentIdsArray = $category->getParentIds(intval($lastcatid));
 $parentIdsnewArray = array_push($parentIdsArray,intval($lastcatid));
  $cat = $objectManager->create('MagentoCatalogModelCategory')->load($parentIdsArray[$i]);
  if($cat->getName() != "Root Catalog"){
  $categoryall .= $cat->getName()."/";
echo $categoryall;

Answered by nihal malik on August 3, 2020

Please try below code to get the first level category collection in your template phtml file:

    $objectManager = MagentoFrameworkAppObjectManager::getInstance();

    //get current category
    $collection = $objectManager->get('MagentoCatalogModelResourceModelCategoryCollectionFactory')->create();
    $collection->addAttributeToFilter('level', array('eq' => 2));

    foreach ($collection as $category) {        

Note: Direct use of objectManager is not recommended. Try to create the function in your block class and use it in the template.

Also, you can get the Parent Categories and check the level in for loop!

use MagentoFrameworkRegistry $registry

public function getCategoryCollection(){

        $currentCategory = $this->_registry->registry('current_category');
                foreach ($currentCategory->getParentCategories() as $parent) {

                    //check level
                    if ($parent->getLevel() == 1) {
                        return $parent->getId();
        return null;

Or try to inject the MagentoCatalogModelResourceModelCategoryCollectionFactory class in your block class

use  MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryCollection

public function getCategoryCollection()
        // $level = 2;
        $collection = $this->_categoryCollection->create()
            // ->setStore($this->_storeManager->getStore())
            ->addAttributeToFilter('level', array('eq' => 2));
            // ->addLevelFilter($level); OR

       return $collection;

Hope this helps!

Answered by Bhaumik Upadhyay on August 3, 2020

    el_magento.catalog_category_entity e1
    el_magento.catalog_category_entity e2
    e1.entity_id = e2.parent_id
    e1.parent_id = 0;

Let me know if you need any further assistance !

Answered by Joao71 on August 3, 2020

Add your own answers!

Related Questions

How to login Magento 2 from external web application

1  Asked on December 14, 2021 by milind-mahajan


Magento2 How to use custom fonts in Homepage

1  Asked on December 14, 2021 by sanjay-shiyal


sorting not working magento 2

1  Asked on December 12, 2021 by ram-pratap-singh-gour


magento2 rest API url, change API url

1  Asked on December 12, 2021


Custom shipping method break cart page render

0  Asked on December 12, 2021 by matheus-prado


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP