2

I want to get the first level parent category of the current category. I have a code but it gets only the parent of the current category:

    protected $_registry;


    public function __construct(
        \Magento\Framework\Registry $registry,      
        \Magento\Framework\View\Element\Template\Context $context
    ) {
        $this->_registry = $registry;
        parent::__construct($context);
    }

    public function getCurrentCategory()
    {
        return $this->_registry->registry('current_category');
    }

    public function getParentCategory()
    {
        if($this->getCurrentCategory()):
            if($this->getCurrentCategory()->getParentCategory()):
                  $this->getCurrentCategory()->getParentCategory()->getId();
            endif;
        endif;
    }
5

you can use to getParentCategories() to collect all parent categories, then loop check with level.

    protected $_registry;

    public function __construct(
        \Magento\Framework\Registry $registry,      
        \Magento\Framework\View\Element\Template\Context $context
    ) {
        $this->_registry = $registry;
        parent::__construct($context);
    }

    public function getCurrentCategory()
    {
        return $this->_registry->registry('current_category');
    }

    public function getLevel1Category(){
        if($this->getCurrentCategory()){
            if($this->getCurrentCategory()->getParentCategories()){
                foreach ($this->getCurrentCategory()->getParentCategories() as $parent) {
                    if ($parent->getLevel() == 1) {
                        // reurns the level 1 category id;
                        return $parent->getId();
                    }
                }
            }
        }
        return null;
    }

if above methods not worked means you can go with getpath() then explode the path with '/'. you will get the root parent category.

i hope this will help you to achieve your goal.

| improve this answer | |
  • I will make a small addition if possible. The method getParentCategories is not public API of CategoryInterface. If you need to use only API and increase upgradability of your module, you can add service, which will use CategoryInterface::getPath and CategoryRepositoryInterface::getLIst with id = parent_ids and level=1 criteria. In this case, you will use only API of Catalog module without private part. – Max Oct 6 '17 at 9:44
  • yes sure max, interesting add a detail answers, its really helps me and others who learn magento2 – Sathish Oct 6 '17 at 9:57
0
<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
    $parCatId = $category->getId(); // current Category ID
    $parCategory = $objectManager->create('Magento\Catalog\Model\Category')->load($parCatId);
    $parent_category = $parCategory->getParentCategory();
    $catName = $parent_category->getName();
    echo "<pre>";
    print_r($catName);
    echo "<pre>";
?>
| improve this answer | |
-1

You can try this:

protected $_categoryCollectionFactory;

public function __construct(
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
)
{
    $this->_categoryCollectionFactory = $categoryCollectionFactory;
}

public function getCategoryCollection($categoryId){
    $categoryFactory = $this->_categoryCollectionFactory->create();
    $category = $categoryFactory->load($categoryId);

    $collection->addAttributeToFilter('level', array('eq' => 1));
    return $collection;
}
| improve this answer | |
  • This code gives current category id. I want to get top level parent category id – Jancy Abraham Oct 6 '17 at 9:08
  • Please review my update above. – TriAnhHuynh Oct 6 '17 at 9:51
  • invalid variable, $collection – jafar pinjar Jun 5 at 17:46

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.