4

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');    
    }
}
8
  • When you execute your code, what is the output ?
    – MGento
    Commented Sep 22, 2017 at 9:34
  • The code will give blank output Commented Sep 22, 2017 at 9:35
  • try to do print_r($objectManager->create('Magento\Framework\Registry')->registry('slct_options'), true); @JancyAbraham
    – Keyur Shah
    Commented Sep 22, 2017 at 9:38
  • @JancyAbraham, please make sure, in your observer's execute function, $observer->getRequest()->getParams() is null or not . Try to print it.
    – MGento
    Commented Sep 22, 2017 at 9:41
  • @MGento , $observer->getRequest()->getParams() contains an array Commented Sep 22, 2017 at 9:49

5 Answers 5

4

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.

5
  • not working :-( Commented Sep 22, 2017 at 10:42
  • did you get it ?
    – MGento
    Commented Sep 25, 2017 at 4:53
  • No,still it is not working. When I remove the die(); then it is not redirected to my page Commented Sep 25, 2017 at 5:17
  • Registry will not work as per the scenario here. You have to use session instead. This could save your time.
    – MGento
    Commented Sep 25, 2017 at 7:07
  • ya, now I am checking with session Commented Sep 25, 2017 at 7:17
3

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.

8
  • I have updated the code, still it is not working. Commented Sep 22, 2017 at 10:03
  • You are trying to get the registry without setting it!! Commented Sep 22, 2017 at 10:09
  • I have set the registry in the observer . Commented Sep 22, 2017 at 10:11
  • I cant see any setRegistry in observer. see my updated answer!! Commented Sep 22, 2017 at 10:12
  • This is my code $this->_registry->register('slct_options', $observer->getRequest()->getParams()); Please check the RestrictAddToCart class Commented Sep 22, 2017 at 10:13
2

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.

1
  • I have updated the code, still it is not working. Commented Sep 22, 2017 at 10:03
1

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

1
  • I have updated the code, still it is not working. Commented Sep 22, 2017 at 10:03
1
$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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.