General

How to Get Current Category in Magento 2

Hello Magento Folks?,

How are things going on? Today in this article I will illustrate to you all How to Get Current Category in Magento 2. Don’t forget to visit our previously published blog How to set Magento 2 recently viewed products. Without further ado let’s begin today’s illustration How to Get Current Category in Magento 2 ?

Introduction:

Mainly, to get current category details in Magento 2 means retrieving all the information that is present on the category page for example current category name, current category id and many other category details. Magento 2 with all the rich features includes the registry is for storing data. Therefore, we will initiate by getting the category data from the registry. Follow the below-given steps to Get current category in Magento 2.

Steps to Get the Current Category in Magento2:

Step 1: To get current category firstly we will need to create one block file on our custom extension

add Blockname.php in the following path

app\code\Vendor\Extension\Block\Blockname.php

now add following code

<?php
namespace Vendor\Extension\Block;

use Magento\Framework\View\Element\Template;

class Blockname extends Template
{

    protected $_registry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
    )
    {
        $this->_registry = $registry;
        parent::__construct($context);
    }

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

Step 2: Now we will get a current category in the template phtml file

if ($currentCategory = $block->getCurrentCategory()) {
    echo $currentCategory->getName() . '<br />';
    echo $currentCategory->getUrl() . '<br />';
}

That’s It.

Final Words:

Hopefully with the help of the above illustration all are able to get current category information in Magento 2. Also, make sure you share the blog with your Magento developer friends. If you have any queries to get current category or related to Magento 2 then comment down in the comment box below I will be happy to help you.

Happy Coding

Click to rate this post!
[Total: 7 Average: 4.7]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

22 hours ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

2 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

4 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

6 days ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

7 days ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

7 days ago