1

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?

  • You can use this module flow. github.com/Vinai/module-current-product-example – Rohan Hapani Jul 12 at 4: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 at 4: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 at 4: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 at 4:46
  • Is it useful for you? Then, I post as answer. – Rohan Hapani Jul 12 at 5:46
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();
  • I cannot use Registry (neither ObjectManger) as it's deprecated in M2.3.1. (mentioned in the question) – Adarsh M Jul 12 at 4:13
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().

  • From where would the id parameter get sent? Also, I need to use in a Block. – Adarsh M Jul 12 at 4:14
  • @AdarshM did you get solution? if not then on which page you want product id? – Mohit Rane Jul 16 at 13:12
  • I want to use in any place. (Block/Plugin/Observer/Controller/etc) – Adarsh M Jul 17 at 8:14
  • in frontend or backend? – Mohit Rane Jul 17 at 9:08
  • In the frontend. – Adarsh M Jul 17 at 11:50
0

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.

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.