Running in real troubles with our products? Get in touch with our Support Team 👉

0
No products in the cart.
  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #9255
    David Hoang
    Keymaster

    How can i get current category in magento2 ?

    I want to get category name and category id in custom phtml file.

    #9257
    David Hoang
    Keymaster

    Try this code. this will definitely help you.

    <?php 
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
        echo $category->getId();
        echo $category->getName();
    ?>
    
    #9259
    David Hoang
    Keymaster

    Magento sets registry for categories being accessed. So, to get currenct category, use following method:

    /**
     * @param \Magento\Framework\Registry $registry
     */
    
    protected $_registry;
    
    public function __construct(
        \Magento\Framework\Registry $registry
    ) {
        $this->_registry = $registry;
    }
    

    and then use:

    $category = $this->_registry->registry('current_category');//get current category
    

    Now you can access the collection and fetch details such as $category->getName()

    #9260
    David Hoang
    Keymaster

    The above to seem correct, but I think that jumping straight to the Registry is not the best approach. Magento provides a Layer Resolver that already encapsulates that functionality. (See the TopMenu Block in the Catalog Plugins)

    I suggest injecting the \Magento\Catalog\Model\Layer\Resolver class and using that to get the current category. Here is the code :

    <?php
    
    namespace FooBar\Demo\Block;
    
    class Demo extends \Magento\Framework\View\Element\Template
    {
        private $layerResolver;
    
        public function __construct(
            \Magento\Framework\View\Element\Template\Context $context,
            \Magento\Catalog\Model\Layer\Resolver $layerResolver,
            array $data = []
        ) {
            parent::__construct($context, $data);
    
            $this->layerResolver = $layerResolver;
        }
    
        public function getCurrentCategory()
        {
            return $this->layerResolver->get()->getCurrentCategory();
        }
    }
    

    Here is what the actual getCurrentCategory() method does in the Resolver Class.

    public function getCurrentCategory()
    {
        $category = $this->getData('current_category');
        if ($category === null) {
            $category = $this->registry->registry('current_category');
            if ($category) {
                $this->setData('current_category', $category);
            } else {
                $category = $this->categoryRepository->get($this->getCurrentStore()->getRootCategoryId());
                $this->setData('current_category', $category);
            }
        }
    
        return $category;
    }
    

    As you can see, it does still use the registry but it provides a fallback in case that fails.

    #9258
    David Hoang
    Keymaster

    No need to use the object manager or inject class. You can use a built-in helper class Magento\Catalog\Helper\Data in the following way.

    <?php 
        $catalogHelperData = $this->helper('Magento\Catalog\Helper\Data');
        $categoryObject = $catalogHelperData->getCategory();
        $categoryId = $categoryObject->getId();
        $categoryName = $categoryObject->getName();
    ?>
    

    This code snippet should work for any phtml (built-in or custom) file which is related to product listing page or product detail page.

    #9256
    David Hoang
    Keymaster

    In *.phtml files at Category page can get Category data with following snippet:

    $currentCategory = $this->helper('Magento\Catalog\Helper\Data')->getCategory();
    
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.