General

How to Get Current Product in Magento 2

Hello Magento Folks,

What are you all up to? Welcome again to the Magento tutorial blog where today’s topic is How to get Current Product in Magento 2.

Many a time you need to get current product details in Magento 2. The current product information like product id, product name, and many other details are displayed on the product page. There are several ways to get current product details. As we know that Magento is built with many features in that case it also provides a feature in which the registry is included for storing data of the current product.

Let’s start with the steps to Get Current Product in Magento 2.

Steps to Get Current Product in Magento 2:

Step 1: Create one block file on our custom extension

add Blockname.php in the following path

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

Now add the 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 getCurrentProduct()
    {
        return $this->_registry->registry('current_product');
    }

}

Step 2: Now, we will get the current product in the template phtml file

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

That’s It

Final Words:

The above described is the easiest way to Get Current Product in Magento 2. Also, share the article with your Magento friends and colleagues. If you have any issues related to Magento 2 then write them down in the comment section below I will solve them for you. Stay tuned and Staf safe.

Happy Coding

Click to rate this post!
[Total: 10 Average: 4.6]
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

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago