{"id":75,"date":"2019-06-02T14:37:18","date_gmt":"2019-06-02T14:37:18","guid":{"rendered":"https:\/\/www.magetrend.com\/blog\/?p=75"},"modified":"2022-05-29T08:44:56","modified_gmt":"2022-05-29T08:44:56","slug":"magento-2-get-current-product","status":"publish","type":"post","link":"https:\/\/www.magetrend.com\/blog\/magento-2-get-current-product\/","title":{"rendered":"How to get current product in Magento 2"},"content":{"rendered":"\n<p>If you are working on Magento 2 project, then many times you need to get current product object. Magento 2 has several different ways to get it. In this article, I would like to overview two, the most popular, ways. The current product in Magento 2 is stored to Magento registry, with the key <span class=\"highlight\">current_product<\/span>. In both ways we will use this registry to get, what we need, but one of the methods is more correct. Let&#8217;s begin with the easiest one.<\/p>\n\n\n<div class=\"simple-container\"> <a class=\"simple-img\" href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_75&utm_campaign=pos1\"><img decoding=\"async\"  src=\"\/blog\/wp-content\/uploads\/prod\/magento-2-pdf-templates.png\" \/><\/a> <div class=\"simple-row simple-title\"><a href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_75&utm_campaign=pos1\">The Most Popular PDF Designer for Magento 2<\/a><\/div> <div class=\"simple-row simple-text\"><a href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_75&utm_campaign=pos1\">with user-friendly PDF editor to build your own PDF look<\/a><\/div> <div class=\"simple-action\"><a href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_75&utm_campaign=pos1\">Read More<\/a> <\/div> <\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>$product = \\Magento\\Framework\\App\\ObjectManager::getInstance()\n    -&gt;get(\\Magento\\Framework\\Registry::class)\n    -&gt;registry('<span class=\"highlight\">current_product<\/span>');<\/code><\/pre>\n\n\n\n<p>This code snippet works in almost all code places where you will paste it. The great thing about it is, that this code snippet is in one piece and it&#8217;s very easy to use. But there is a better way.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The right way to get current product<\/h2>\n\n\n\n<p>According to Magento, it&#8217;s not recommended to use object manager. The right way is dependency injection method. As I already mentioned,  current product is stored in Magento registry. So we need to include registry object in class constructor and call it directly. Here is the code example how to get current product using dependency injection.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public $registry;\n\npublic function __construct(\\Magento\\Framework\\Registry $registry)\n{\n    $this-&gt;registry = $registry;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$product = $this-&gt;registry-&gt;registry('<span class=\"highlight\">current_product<\/span>');<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Code example: how to get current product in .phtml template<\/h3>\n\n\n\n<p>Our block class should look like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace Magetrend\\HelloWorld\\Block;\n\nclass Attachment extends \\Magento\\Framework\\View\\Element\\Template\n{\n    public $registry;\n\n    public function __construct(\n        \\Magento\\Framework\\View\\Element\\Template\\Context $context,\n        \\Magento\\Framework\\Registry $registry,\n        array $data = []\n    ) {\n        $this-&gt;registry = $registry;\n        parent::__construct($context, $data);\n    }\n\n    public function getCurrentProduct()\n    {\n        return $this-&gt;registry-&gt;registry('<span class=\"highlight\">current_product<\/span>');\n    }\n}<\/code><\/pre>\n\n\n\n<p>then we can make a call in our template .phtml file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php $currentProduct = $block-&gt;getCurrentProduct(); ?&gt;<\/code><\/pre>\n\n\n\n<p>to get product ID, product name or other product information you can use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php echo $currentProduct-&gt;getId(); ?&gt;\n&lt;?php echo $currentProduct-&gt;getName(); ?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>No changes in product page<\/strong><\/h3>\n\n\n\n<p>if you have added the code in .phtml file and don&#8217;t see any changes in fronted, probably it&#8217;s just need to clear the cache.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>That&#8217;s all for this post. Let me know, in the comments bellow,  if something was missed. I will be more than happy to get your feed and append this post.<\/p>\n\n\n<div class=\"simple-container\"> <a class=\"simple-img\" href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_75&utm_campaign=pos2\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/prod\/magento-2-email-templates.png\" \/><\/a> <div class=\"simple-row simple-title\"><a href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_75&utm_campaign=pos2\">Responsive Emails Templates for Magento 2<\/a><\/div> <div class=\"simple-row simple-text\"><a href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_75&utm_campaign=pos2\">Our #1 bestseller. With user-friendly template editor!<\/a><\/div> <div class=\"simple-action\"><a href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_75&utm_campaign=pos2\">Read More<\/a> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you are working on Magento 2 project, then many times you need to get current product object. Magento 2 has several different ways to get it. In this article, &hellip; <\/p>\n<div class=\"grid-footer\"><a href=\"https:\/\/www.magetrend.com\/blog\/magento-2-get-current-product\/\" class=\"more-link\">Read More <i class=\"mticon-arrow\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":263,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts\/75"}],"collection":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/comments?post=75"}],"version-history":[{"count":15,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"predecessor-version":[{"id":480,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions\/480"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/media\/263"}],"wp:attachment":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}