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

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

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