13

Any one know how I can get the category id using current category registry in Magento 2?

0

I think You have done in your controller may be you should get the object manager to be initialized by use of context class in the __construct method .

  • Hi i already call construct method in my block class file please check below link for more detail codebeautify.org/alleditor/0b796a And thanks for help. – Pratik Aug 24 '15 at 8:46
  • This should not be the accepted answer. You should not use Objectmanager in in the way described. – seanbreeden Oct 23 '18 at 12:43
22

If you need the category Id in Magento2 ,then you can get the values using the following steps to be followed

1.Include the use Magento\Framework\Registry in your class file.

<?php
/**
 * class file
 */

namespace Vendor\Module\Model;

use Magento\Framework\Registry;

...

2.Create an object for that using object Manager or else if your using it in the controller then assign in your __construct() function as \Magento\Framework\Registry $registry:

...

/**
 * @var Registry
 */

class BlueLine
{
    ...
    private $registry;
    ...

    public function __construct(Registry $registry) 
    {
        $this->registry = $registry;
    }

    ...

3.Then you can simply use it with the class as:

$category = $this->registry->registry('current_category');

echo $category->getId();

For the further Reference in Magento2 Implementation of this concept refer the class file and function called public function _initCategory(). In this method they are registering the current category.

  • Hi I follow step and check result its showing There has been an error processing your request Object DOMDocument should be created. – Pratik Aug 24 '15 at 6:10
  • Your private property is in the wrong place. – LM_Fielding Dec 12 '16 at 12:12
10

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();
?>
3

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();
    }

    public function getCurrentCategoryId()
    {
        return $this->getCurrentCategory()->getId();
    }
}

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.

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.