12

I am new to magento2 and currently I'm building a custom module and i want to get the product ID/SKU from the catalog. I'm trying to call using a function located in the Block folder. please advise!

7 Answers 7

39

Try this:

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

OR

Add bellow code in your block file.

for example app/code/AR/CustomModule/Block/CustomBlock.php

<?php
namespace AR\CustomModule\Block;
class CustomBlock extends \Magento\Framework\View\Element\Template
{
    protected $_registry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,       
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {       
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }

    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getCurrentCategory()
    {       
        return $this->_registry->registry('current_category');
    }

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

}
?>

Printing current product data in your template (custom.phtml) file

if ($currentProduct = $block->getCurrentProduct()) {
    echo $currentProduct->getName() . '<br />';
    echo $currentProduct->getSku() . '<br />';
    echo $currentProduct->getId() . '<br />';       
}
4
  • This works like a charm! Do you mind explaining about this registry?? is there a developer's guide on this area??
    – ming
    Apr 6, 2016 at 4:49
  • 3
    Is there any alternate method to get product id apart from registry?
    – Sushivam
    Aug 23, 2016 at 10:30
  • 1
    Registry is deprecated. What is the best practice way to achieve this?
    – Akif
    Apr 24, 2021 at 20:41
  • 1
    wow greate soluions, work for me thanks @abdul May 11, 2022 at 12:19
2
<?php
     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
     $productid = $this->getRequest()->getParam('id');                                       
     $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productid);
     echo $product;
?>                                                                          

use this code for to get current product id

1
  • It's a good answer. You just need to check if you're on product detail page and then get ID from the request payload.
    – Alok Patel
    Apr 17, 2021 at 6:38
0
<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
echo $product->getId();
echo $product->getName();
?>

This works for me.

.

1
  • don't use object manager directly
    – Lorenzo
    Aug 10, 2017 at 11:56
0

You could try $product = $this->abstractProduct->getProduct();

with \Magento\Catalog\Block\Product\AbstractProduct $abstractProduct

worked for me :)

1
  • its not working for product listing page Oct 2, 2019 at 4:59
0
<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
    echo $product->getId();
    echo $product->getName();
    echo $product->getSku();

?>
0

$productid=22501; $objectManager = $bootstrap->getObjectManager(); $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

 $state = $objectManager->get('Magento\Framework\App\State');
 $state->setAreaCode('frontend');

 $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productid);
 
 $data = json_encode($product->getData());

 print_r($data);
-4

Try this:

<?php  
   $productId = 8;
   $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
   $currentproduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
   echo $currentproduct->getName(); 
?>
4
  • Hey, thanks for the reply! I think i have phrased my question wrongly. My current modules is just an add on information for the product, what i really meant is that how do i get the ID of the current page product? example, in "localhost/test/product-A.html" how can i retrieve the id for this particular product
    – ming
    Apr 6, 2016 at 4:16
  • i get Class 'Magento\Framework\App\ObjectManager not found
    – Sushivam
    Aug 2, 2016 at 5:27
  • i m trying to get in my custom Model as : $productId = 1; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $currentproduct = $objectManager->create('Vendor\Module\Model\Queue')->load($productId); echo '<pre>';print_r($currentproduct->getEntityId());die; i get call to dispatch() on null ..pls help
    – Sushivam
    Aug 22, 2016 at 7:17
  • How to dynamically get the $productId = 8;
    – Sushivam
    Aug 23, 2016 at 10:07

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.