Magento – Magento 2 : How to get current product programmatically in a helper

helpermagento2productregistryurl

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

Best Answer

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');
}