{"id":1782,"date":"2021-05-22T11:59:36","date_gmt":"2021-05-22T11:59:36","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-current-product-id-in-magento-2\/"},"modified":"2025-07-16T17:46:51","modified_gmt":"2025-07-16T12:16:51","slug":"get-current-product-id-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-current-product-id-in-magento-2\/","title":{"rendered":"How to Get Current Product ID in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2\u00a0is a flexible and feature-rich eCommerce platform with many robust features that allow you to customize your online store as your business requirement!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can customize its admin panel as well as frontend using various tactics.&nbsp;One of the basic requirements while applying product-related customization is to&nbsp;get current product ID in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The admin can create&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-product-types\/\" target=\"_blank\" rel=\"noreferrer noopener\">six different types of products in Magento 2<\/a>&nbsp;store, and each time when a product is created, a unique id gets generated for that product.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In such a case, you have to get the product id of that product before doing further customization!&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One can get the current product Id using two methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using object manager<\/li>\n\n\n\n<li>Using block<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Besides, you may also need to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/get-parent-product-id-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">get parent product id in Magento 2<\/a>, you can refer to my solution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Current Product ID in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. <strong>Using Object Manager<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\n$product = $objectManager->get('Magento\\Framework\\Registry')->registry('current_product');\/\/get current product\necho $product->getId();\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. <strong>Using block<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the below code in your block file.<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Block;\nclass BlockClass extends \\Magento\\Framework\\View\\Element\\Template\n{\n    protected $registry;\n\n    public function __construct(\n        \\Magento\\Backend\\Block\\Template\\Context $context,\n        \\Magento\\Framework\\Registry $registry,\n        array $data = []\n    )\n    {\n        $this->registry = $registry;\n        parent::__construct($context, $data);\n    }\n\n    public function _prepareLayout()\n    {\n        return parent::_prepareLayout();\n    }\n\n    public function getCurrentProduct()\n    {\n        return $this->_registry->registry('current_product');\n    }\n}\n\n?><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Call function in your .phtml file:<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$currentProduct = $block->getCurrentProduct();\necho $currentProduct->getId();\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do consider sharing this post with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2\u00a0is a flexible and feature-rich eCommerce platform with many robust features that allow you to customize your online store as your business requirement! You&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1782","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1782"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1782\/revisions"}],"predecessor-version":[{"id":18066,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1782\/revisions\/18066"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}