How to get current product, category in Magento 2

If you are wondering how to get/print the information of the current product** and even **the current category, the topic is all things you need to find. Two steps are recommended in this topic, please get them and do any thing you need.

Overview of getting current product, current category in Magento 2

  • Step 1: Declare in Mageplaza_HelloWorld
  • Step 2: Print out the current product in the template phtml file
  • Step 3: Print out current category in the template phtml file

    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: Print out the 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: Print out 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 guide to get current product and current category in Magento 2. If that you have any queries about the article or any questions in general, use the comment section below!



Next tutorial:

Module Development Series



Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
how-to
get
current
product
current
category

Comments for How to get current product, category in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

Shop By Brand

$99
3 reviews

People also searched for:

  • magento 2 get current product
  • category
  • magento 2 how to get current product
  • category
  • get current product magento 2
  • get current category in magento 2