BE SMART, hand over your headache to Rootways and FOCUS ON YOUR BUSINESS.

How to get current product data in Magento 2

How to get current product data in Magento 2

If you are working on Magento 2 project, then many times you need to get the current product object. Magento 2 has several different ways to get current product data. In this blog post, We would like to overview the most popular ways. The current product in Magento 2 is stored in the Magento registry, with the key current_product. In both ways, we will use this registry to get, what we need, but one of the methods is more correct. Let’s begin with the easiest one.

$product = \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Framework\Registry::class) ->registry('current_product');

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’s very easy to use.

June 16, 2021
Did you like this post?
0
0