Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to get Current Product & Current Category in Magento 2

Vinh Jacker | 03-17-2025

Get current product, category

Information such as the product name, product ID, product price, or category URLs is vital to any Magento 2 online merchants, as it allows them to have better access to sufficient information to implement suitable business strategies. There are several feedbacks of how confusing getting the current product and category in Magento 2 may get, so here we are to guide you to the right path. Let’s walk through the easiest and most efficient ways to get the current product in Magento 2. Ready? Let’s jump in!

3 Steps to Get Current Product & Current Category in Magento 2

Step 1: Declare in Mageplaza_HelloWorld

You will use a block class of the module Mageplaza_HelloWorld, then possibly inject the object of \Magento\Framework\Registry in the constructor of the module’s block class.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
        protected $_registry;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {        
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }
    
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
    
    public function getCurrentCategory()
    {        
        return $this->_registry->registry('current_category');
    }
    
    public function getCurrentProduct()
    {        
        return $this->_registry->registry('current_product');
    }    
    
}
?>

Step 2: Get current product in the template phtml file

The current product includes the following information: name, SKU, final price, URL and associated category IDs. And the current category comes with the information as name and URL. Now please run the command to print out the current product and the current category.

$myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('Mageplaza\HelloWorld\Block\HelloWorld');
 

// print current product data
if ($currentProduct = $myBlock->getCurrentProduct()) {
    echo $currentProduct->getName() . '<br />';
    echo $currentProduct->getSku() . '<br />';
    echo $currentProduct->getFinalPrice() . '<br />';
    echo $currentProduct->getProductUrl() . '<br />';
    print_r ($currentProduct->getCategoryIds()) . '<br />';    
}

Step 3: Get current category in the template phtml file

$myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('Mageplaza\HelloWorld\Block\HelloWorld');
 
// print current category data
if ($currentCategory = $myBlock->getCurrentCategory()) {    
    echo $currentCategory->getName() . '<br />';
    echo $currentCategory->getUrl() . '<br />';        
}
 

How to Get Current Product ID in Magento 2

To get the Current Product ID in Magento 2, you can use one of the following approaches:

This method is the preferred way as it adheres to Magento 2 best practices by avoiding the direct use of ObjectManager.

Template file code:

<?php
$currentProductId = $block->getCurrentProductId();
if ($currentProductId) {
    echo "Current Product ID: " . $currentProductId;
} else {
    echo "No current product is available.";
}
?>

This method uses the ObjectManager directly and is not considered best practice because it reduces code maintainability and testability. Only use it if Dependency Injection is not feasible in your case.

<?php
use Magento\Framework\App\ObjectManager;
$objectManager = ObjectManager::getInstance();
$registry = $objectManager->get(\Magento\Framework\Registry::class);
$currentProduct = $registry->registry('current_product');
if ($currentProduct) {
    echo "Current Product ID: " . $currentProduct->getId();
} else {
    echo "No current product is available.";
}
?>

3. From Product Pages Using $block->getCurrentProduct()

If you’re working in a block or template on a product page, you can use getCurrentProduct() if available in your custom block or inherited block.

<?php
$currentProduct = $block->getCurrentProduct();
if ($currentProduct) {
    echo "Current Product ID: " . $currentProduct->getId();
} else {
    echo "No current product is available.";
}
?>

Conclusion

This is the complete guide to get the current product and current category in Magento 2. We tried our best to make everything short and concise for you, so give it a try! Feel free to share it with anyone who is also struggling with this. If you have any queries about the article or any questions in general, use the comment section below!

Explore how to get categories from specific product in Magento 2

Recommend Topics

x
    Jacker

    Jacker is the Chief Technology Officer (CTO) at Mageplaza, bringing over 10 years of experience in Magento, Shopify, and other eCommerce platforms. With deep technical expertise, he has led numerous successful projects, optimizing and scaling online stores for global brands. Beyond his work in eCommerce development, he is passionate about running and swimming.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services