0

I want to display product id on product view page when user click on product.

For ex :- This product id is 1. When This page is display on this page I want to display product id 1.

How can I get ?

Please help me.....

enter image description here

5
  • which position, you want to display product id.
    – Sohel Rana
    Commented May 4, 2017 at 4:56
  • 1
    which product user view...that product's id want to display on that product view page.... Commented May 4, 2017 at 5:02
  • 1
    want to display in alert box..when page load... Commented May 4, 2017 at 5:25
  • You can use $_product->getId() on phtml where you want product id. ofcourse related to product detail page.
    – Kul
    Commented May 4, 2017 at 5:45
  • 1
    can you give me answer with code? @Kul Commented May 4, 2017 at 5:52

2 Answers 2

5
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();

$registry = $objectManager->get('\Magento\Framework\Registry');

$currentProduct = $registry->registry('current_product');

echo $currentProduct->getId();

You can get current product id using this code.

1

You can use below code on template file from here . Please use your theme and if you dont have files here copy from vendor.

app/design/frontend/package/theme/Magento_Catalog/templates/product/view/

Packag and theme should be change as per your theme Add below code to get product id

<?php echo $_product->getId(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.