Question

I want to set an array in registry in controller_action_predispatch_checkout_cart_add event observer and read this in a phtml page.

The following code will give blank result .

=> Code :

namespace NameSpace\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class RestrictAddToCart implements ObserverInterface
{
    protected $_request;  

    protected $_product;  

    protected $_responseFactory;

    protected $_url;
    protected $_registry;

    public function __construct(
        RequestInterface $request,
        \Magento\Catalog\Model\Product $product,
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\UrlInterface $url
    )
    {
        $this->_request = $request;
        $this->_product = $product;
        $this->_registry = $registry;
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {          
        $this->_registry->register('slct_options', $observer->getRequest()->getParams());
        $customRedirectionUrl = $this->_url->getUrl('my-page');            
        $this->_responseFactory->create()->setRedirect($customRedirectionUrl)->sendResponse();     
        die(); 
        return $this;
    }
}

=> phtml :

<?php 
print_r( $block->getRegisterData());     
 ?>

=> Block :

namespace Namespace\Module\Block;

use \Magento\Framework\View\Element\Template;

class Customblock extends Template
{    
    protected $_registry;

    public function __construct(
        Template\Context $context,
        \Magento\Framework\Registry $registry,
        array $data = [])
    {
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }           
     public function getRegisterData()
    {         
        return $this->_registry->registry('slct_options');    
    }
}
Was it helpful?

Solution

The registry is getting cleared after the server sends a response. Each new request starts with an empty registry.

If you want to transfer data from one request to another you should use the session instead.

OTHER TIPS

The registry will load once the controller is loaded. One more point, registry function should be called in block file only. It is the best way of doing.

So as you are calling the registry inside the controller, it wont work in that way. You need to specify block file and then you access that value to template from block. Even template definition and values should be passed through block only.

Edit:

/**
  * @var \Magento\Framework\Registry
  */

 protected $_registry;

 /**
 * ...
 * ...
 * @param \Magento\Framework\Registry $registry,
 */
public function __construct(
    ...,
    ...,
    \Magento\Framework\Registry $registry,
    ...
) {
    $this->_registry = $registry;
    ...
    ...
}

 /**
 * Setting custom variable in registry
 *
 */

public function setCustomVariable()
{
     $this->_registry->register('custom_var', 'Added Value');
}

/**
 * Retrieving custom variable from registry
 * @return string
 */
public function getCustomVariable()
{
     return $this->_registry->registry('custom_var');
}

Here you can see that I set the registry with setCustomVariable() this function and I am trying to pull the registry with this getCustomVariable().. Now we need to call this getCustomVariable() in phtml file. Now it will work.

Firstly, using objectManager in your code is bad. There are places you are permitted to do that, do your research.

I personally think, you need to create a block and define a function that gets the registry value from your block, that will be accessible in your template by calling the block's function

You can get the registry value as below

<?php   

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$slct_options = $objectManager->create("Magento\Framework\Registry")->registry('slct_options');

Note: It's a good practice not to use ObjectManager directly in phtml file. Do it by using dependency injection.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$slct_options = $objectManager->get("Magento\Framework\Registry")- >registry('slct_options');

use get instead of create.

It create new object you will get null values

as previously said it is last option to use object manager try to add in constructor.

for reference

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top