1

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

root_catalog
    |-First  |-Shoes
             |-T-shirts
    |-Brands
    |-Demo
    |-Test

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

1

3 Answers 3

0
SELECT 
    e2.* 
FROM 
    el_magento.catalog_category_entity e1
INNER JOIN
    el_magento.catalog_category_entity e2
ON 
    e1.entity_id = e2.parent_id
Where
    e1.parent_id = 0;

Let me know if you need any further assistance !

0

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

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

    //get current category
    $collection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory')->create();
    $collection->addAttributeToFilter('level', array('eq' => 2));

    foreach ($collection as $category) {        
        print_r($category->getData());
    }
?>

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 \Magento\Framework\Registry $registry
...

public function getCategoryCollection(){

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

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

Or try to inject the \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory class in your block class

use  \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection

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

       return $collection;
    }

Hope this helps!

7
  • it's not working @bhaumik Commented Jul 9, 2020 at 12:06
  • Are you trying in a template or block class? Commented Jul 9, 2020 at 12:11
  • i am trying Dependency Injection @bhaumika Commented Jul 9, 2020 at 12:15
  • Please check the updated answer. Try to inject the \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory class and get the collection using it. Commented Jul 9, 2020 at 12:26
  • Does it work for you? @RavindrasinhZala Commented Jul 9, 2020 at 12:44
0

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 = \Magento\Framework\App\ObjectManager::getInstance();
$category_Ids = $block->getProduct()->getCategoryIds(); /*will return category ids array*/
$index = count($category_Ids)-1;
$lastcatid = $category_Ids[$index];
$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');// 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));
for($i=0;$i<count($parentIdsArray);$i++){
  $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($parentIdsArray[$i]);
  if($cat->getName() != "Root Catalog"){
  $categoryall .= $cat->getName()."/";
  }
}
echo $categoryall;
1
  • not use object manager.i am trying Dependency Injection thorw. Commented Jul 9, 2020 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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