0

I am trying to get the registry but its always returning null and i dont know why

class Extension extends \Magento\Framework\View\Element\Template  {

        protected $_urlBuilder;
        protected $_storeManager;
        protected $productRepository;
        protected $_coreRegistry;
        public function __contruct(

            \Magento\Framework\UrlInterface $urlBuilder,
            \Magento\Framework\Registry $registry,
            \Magento\Store\Model\StoreManagerInterface $storeManager

        ){

            $this->_urlBuilder = $urlBuilder;
            $this->_storeManager=$storeManager;

            $this->_coreRegistry = $registry;

        }



    const ICON_EXTENSION = '.gif';
    const URL_ICON  =  'images/productextension/';


public function getProduct()
{
    return $this->_coreRegistry->registry('current_product');
}

Why is my registry null?

0

Check below code

try to create a new block file and replace my code and get the current product id. if it works then check in your code where you wrong.

<?php 
namespace Vedor\Module\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Page\Config;
use Magento\Framework\Registry;

class ProductDetailNew extends Template
{
    private $_registry;
    /**
     * @param Context $context
     * @param Config $pageConfig
     * @param Registry $registry
     * @param array $data
     */
    public function __construct(
        Context $context,
        Config $pageConfig,
        Registry $registry,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->pageConfig = $pageConfig;
        $this->_registry = $registry;
    }

    /**
    * @return Int
    **/
    public function getCurrentProductId()
    {        
        return $this->_registry->registry('current_product')->getId();
    } 
}
?>

I hope this will help you.

1
  • I have other question, why does my layout breaks when i add an block into my catalog_product_view.xml? After adding 1 block my page layout converts to 3 columns layout instead of 1 column why is that? – Ifaruki May 8 '20 at 8:19

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.