How to Retrieve Current Category Data in Magento 2

Hi, Magento Developers, We will explore how to retrieve current category details in Magento 2 on Phtml and controller pages. First, we’ll create a custom block and define the Register to obtain current category data through the Registry Object.

Additionally, if you’re curious about how Registry works in Magento and its significance, don’t miss our detailed exploration in another blog post. You can find more insights on Registry by clicking here

Create a custom block 

example path:- app/code/Lokesh/Module/Block/CurrentCategory.php

<?php
namespace Lokesh\Module\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Registry;

class CurrentCategory extends Template
{
    protected $_registry;

    public function __construct(
        Context $context,
        Registry $registry,
        array $data = []
    ) {
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }

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

Add your block to layout XML

 example path:- app/code/Lokesh/Module/view/frontend/layout/catalog_category_view.xml

<referenceContainer name="content">
    <block class="Lokesh\Module\Block\CurrentCategory" name="current.category"     template="Lokesh_Module::current_category.phtml"/>
</referenceContainer>

Create the template file

 example path:- app/code/Lokesh/Module/view/frontend/templates/current_category.phtml

<?php
$currentCategory = $block->getCurrentCategory();
if ($currentCategory) {
    echo $currentCategory->getName();
    echo $currentCategory->getId();
    echo $currentCategory->getDescription();
    echo $currentCategory->getUrl(); 
}

In a controller, You can access the Registry directly in your controller to retrieve the current category.

 example path:- app/code/Lokesh/Module/Controller/Index/Index.php

<?php
namespace Lokesh\Module\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Registry;

class Index extends Action
{
    protected $_registry;

    public function __construct(
        Context $context,
        Registry $registry
    ) {
        $this->_registry = $registry;
        parent::__construct($context);
    }

    public function execute()
    {
        $currentCategory = $this->_registry->registry('current_category');
        if ($currentCategory) {
            // Do something with the category
        }
    }
}

Note: Using the Registry to retrieve the current category is a common approach, but it is not recommended to rely on the Registry extensively. If you need to access the current category frequently across different areas of your application, consider refactoring your code to use more specific and decoupled methods of accessing category data.

Thank you for reading this Article! Feel free to share your thoughts or ask any questions in the comments section below and spread the word by sharing. Your engagement is appreciated!

Leave a reply

Your email address will not be published. Required fields are marked *

Cookies Notice

Our website use cookies. If you continue to use this site we will assume that you are happy with this.