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 here.
Method to Get Current Product ID in Magento 2
- Using Object Manager12345<?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.
class BlockClass extends \Magento\Framework\View\Element\Template
public function __construct(
array $data = 
$this->registry = $registry;
public function _prepareLayout()
public function getCurrentProduct()
- Call function in your .phtml file:
$currentProduct = $block->getCurrentProduct();
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.