Magento – How to get product id in product detail page

magento2producttemplate

How to get product ID in Magento_Catalog/template/view/my_custom_template.phtml file?

Trying this one:

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

But not working!

Best Answer

Use block class Magento\Catalog\Block\Product\View for render your phtml file.

You can get product Id by below code

$block->getProduct()->getId();
Related Topic