0

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

  • On which section you want to display the product id? – Sukumar Gorai Aug 27 '18 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 – 55840 Aug 27 '18 at 9:23
  • Don't use object manager. – Sukumar Gorai Aug 27 '18 at 9:38
  • try this : blog.chapagain.com.np/… – Magento Developer Aug 27 '18 at 10:09
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.

| improve this answer | |
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();
?>
| improve this answer | |

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.