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

  1. Using Object Manager
  2. Using block
  • Use the below code in your block file.
  • Call function in your .phtml file:
That’s it.

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.

(based on 23 Reviews)
How to Get Current Product ID in Magento 2

Krishna Rajyaguru

Krishna is a Magento developer at Meetanshi. She is always ready to learn new things. Apart from work, she likes to read books and spend time with family.


Leave a Reply

Your email address will not be published. Required fields are marked *