How to get Current Product & Current Category in Magento 2

Information such as the product name, product ID, product price, or category URLs are 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. Two steps are recommended in this topic, please follow them.

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 />';        
}
 

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

Image Description
Hello, I'm the Chief Technology Officer of Mageplaza, and I am thrilled to share my story with you. My deep love and passion for technology have fueled my journey as a professional coder and an ultra-marathon runner. Over the past decade, I have accumulated extensive experience and honed my expertise in PHP development.

People also searched for

  • magento 2 get current product
  • magento 2 get current category
  • get current product magento 2
  • get current product id in magento 2
  • get current product in magento 2
  • how to get current product in magento 2
  • get current category magento 2
  • magento2 get current product
  • magento 2 get current product in block
  • magento 2 get current product in phtml file
  • get current category in magento 2
  • magento 2 get current product id
  • magento 2 get current category id in list phtml
  • magento 2 get current category in phtml
  • get product category magento 2
  • get current category id in magento 2
  • magento 2 get current category id
  • how to get current product id in magento 2
  • magento 2 get product id
  • magento2 get current category
  • 2.3.x, 2.4.x
x