How to Get Current Product ID in Magento 2
Magento 2 is a flexible and feature-rich eCommerce platform with many robust features that allow you to customize your online store as your business requirement!
You can customize its admin panel as well as frontend using various tactics. One of the basic requirements while applying product-related customization is to get current product ID in Magento 2.
The admin can create six different types of products in Magento 2 store, and each time when a product is created, a unique id gets generated for that product.
Now, what if you want a product page with a unique look and feel for the particular product than the default product page of Magento 2?
In such a case, you have to get the product id of that product before doing further customization!
One can get the current product Id using two methods:
- Using object manager
- Using block
However, it is best practice not to reference the object manager class directly. You can use one of the below methods as per your business requirements.
Besides, you may also need to get parent product id in Magento 2, you can refer to my solution.
Method to Get Current Product ID in Magento 2
- Using Object Manager
12345<?php$objectManager = \Magento\Framework\App\ObjectManager::getInstance();$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current productecho $product->getId();?> - Using block
- Use the below code in your block file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php namespace Vendor\Module\Block; class BlockClass 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 getCurrentProduct() { return $this->_registry->registry('current_product'); } } ?> |
- Call function in your .phtml file:
1 2 3 4 |
<?php $currentProduct = $block->getCurrentProduct(); echo $currentProduct->getId(); ?> |
If you have queries regarding this solution, feel free to ask in the Comments section below.
I would be happy to answer your question.
Do consider sharing this post with Magento Community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
Hello please change your _registry name to registry into getCurrentProduct() function. there are show error when above code used.
Hello Mahesh,
https://drops.meetanshi.com/3K5eh7
Please replace the below code
$this->_registry->registry(‘current_product’);
with
$this->registry->registry(‘current_product’);
Thank You
Hi i have created block and this block function i have called product detail page custom phtml , i am not getting current product id
Hello Rajesh,
Please try after cleaning the cache.
Or use the code to call function in your .phtml file
Thank You.