Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current category in magento2?

How can i get current category in magento2 ?

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

like image 286
Makwana Ketan Avatar asked Jan 20 '16 04:01

Makwana Ketan


People also ask

How do you get current time zone in Magento website?

Magento Per Store Timezone Settings If you navigate to “System->Configuration->General->Locale Options->Timezone” in Admin area of Magento, you'll see that you can change Timezone for each Website you have.

How can I get category custom attribute value in Magento 2?

You can use Category's CollectionFactory class and select all attributes by using a star (*) symbol in addAttributeToSelect method. You can use this code example below in your class. protected $_categoryFactory; public function __construct( // ...


2 Answers

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.

like image 162
drew7721 Avatar answered Sep 18 '22 17:09

drew7721


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.

like image 30
Milan Chandro Avatar answered Sep 19 '22 17:09

Milan Chandro