0

Help me with these two type please:

  • Using object manager

  • Using block

2
2

Reference link

Using Object Manager

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

Using Block

<?php

namespace Vendor\Module\Block;
class BlockClass 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 getCurrentProduct()
     {
           return $this->_registry->registry('current_product');
     }
}

?>
1

Using object manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
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.