3

Not a duplicate question, as the referenced original question uses deprecated classes.

I want to get the current product in a Block / Model in Magento 2.3.1. I cannot directly use / inject / extend any deprecated classes like Registry / AbstractProduct. Must use DI and not ObjectManager

What is the best alternative for this?

Adarsh M
  • 584
  • 1
  • 4
  • 22
  • 1
    You can use this module flow. https://github.com/Vinai/module-current-product-example – Rohan Hapani Jul 12 '19 at 04:33
  • @RohanHapani that seems like a manual fix? Registry had a quick solution for this and I'm not able to find a direct and recommended alternative for it. – Adarsh M Jul 12 '19 at 04:36
  • No. It's proper way for get current product. But, still you can use by registry also. It's working in 2.3.1 If still not working then, you can use this module – Rohan Hapani Jul 12 '19 at 04:38
  • I guess there's no direct alternative then? (Only a manual workaround?) And yeah, deprecated classes will work, but it is not good to use them anyways. – Adarsh M Jul 12 '19 at 04:46
  • Is it useful for you? Then, I post as answer. – Rohan Hapani Jul 12 '19 at 05:46
  • @RohanHapani No, I already came across that, but it's a manual fix for the issue. – Adarsh M Jul 12 '19 at 05:50

3 Answers3

3

There is a solution suggested at https://www.atwix.com/magento-2/alternatives-for-deprecated-registry-class-magento-2-3/

Also, there is discussion in the comment section of the article on the other alternative options.

1

You can get id in params,

$this->getRequest()->getParams();

Or

$this->request->getParam('product_id'); // Use this class in construct() \Magento\Framework\App\RequestInterface

In your Controller file

protected $request;

public function __construct(
        \Magento\Framework\App\RequestInterface $request
    ) {
        $this->request = $request;
    }

public function execute()
{
    $this->request->getParam('product_id');
}

To get id in getParam() you need to pass it as hidden field in phtml file

For Example:-

in phtml file

<input type="hidden" name="product_id" value=""> <!-- value should be your <?php echo $product_id ?> -->

In Observer & Plugin File:- It depends on which event you want to observe and same goes for the plugin. If you're passing as hidden field and calling a observer on that particular event, you can get id as i mentioned above.

In Block file:- Since you don't want to use Registry, you can try using $this->request->getParams().

Mohit Rane
  • 1,955
  • 1
  • 13
  • 47
-2

You can get Current Product by creating custom module or by object manager, But I am not refer to use as Object Manager

1. Using Module

Create file app/code/Vendor/Module/Block/Product.php and add following Code.

<?php
namespace Vendor\Module\Block;
class Product 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');
    }    

}
?>

Now you can get the current product in phtml file.

// print current product data
$currentProduct = $block->getCurrentProduct();
echo $currentProduct->getName()

2. Using Object Manager directly phtml

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$registry = $objectManager->get('\Magento\Framework\Registry');
$currentProduct = $registry->registry('current_product');
echo $currentProduct->getName();
echo $currentProduct->getSku();
Kishor Thummar
  • 3,000
  • 1
  • 9
  • 18
  • 1
    I cannot use Registry (neither ObjectManger) as it's deprecated in M2.3.1. (mentioned in the question) – Adarsh M Jul 12 '19 at 04:13