2

Could someone advise what's the best way to get the current product in a custom helper? Basically if I am a product page I need to get it's sku

1
  • Helpers are usually used as elements/methods that are global and always available. Helpers can be called anywhere and you only need to inject them in the class. I think you should create a block class which should have the code as per shared in answer below. That shall solve the problem for you.
    – Gagan
    Sep 13 '18 at 13:28
3
protected $_registry;

public function __construct(
    \Magento\Framework\App\Helper\Context $context        
    \Magento\Framework\Registry $registry
)
{        
    $this->_registry = $registry;
    parent::__construct($context);
}

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

To get the current product:

use block class: Magento\Catalog\Block\Product\View

Get product using: $block->getProduct() in your phtml or use the class and get in the block file.

Hope it Helps.Happy coding!!

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.