Question

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;
    }
Was it helpful?

Solution

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.

OTHER TIPS

<?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>";
?>

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top