0

I dont know how to implement this task. I want to display product Id in product view page. Can you please help me?

4
  • On which section you want to display the product id? Aug 27, 2018 at 9:15
  • $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product echo $product->getId(); // getting product id
    – Mahendra
    Aug 27, 2018 at 9:23
  • Don't use object manager. Aug 27, 2018 at 9:38
  • try this : blog.chapagain.com.np/… Aug 27, 2018 at 10:09

2 Answers 2

0

Check for the product object in the file where you want to display product id .

And use below code to get the product id. If you have product object as $product.
Then use

 $product->getId();

That will give you product id.

0

1) Create a Block

<?php
namespace vendor\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    protected $registry;

    public function __construct(   
        \Magento\Framework\Registry $registry,

    )
    {       
        $this->registry = $registry;

    }


    public function getCurrentProduct()
    {       
        return $this->registry->registry('current_product');
    }   

}
?>

Get product information In your phtml file

<?php
  $currentProduct = $block->getCurrentProduct();
            echo $currentProduct->geId();
            echo $currentProduct->geName();
 ?>

If you want to get in current product information in phtml you can use following code.

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $currentProduct = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product information
    echo $currentProduct>getId();
    echo $currentProduct>getName();
?>

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.