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');    
    }
}
  • When you execute your code, what is the output ? – MGento Sep 22 '17 at 9:34
  • The code will give blank output – Jancy Abraham Sep 22 '17 at 9:35
  • try to do print_r($objectManager->create('Magento\Framework\Registry')->registry('slct_options'), true); @JancyAbraham – Keyur Shah Sep 22 '17 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 Sep 22 '17 at 9:41
  • @MGento , $observer->getRequest()->getParams() contains an array – Jancy Abraham Sep 22 '17 at 9:49
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.

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

| improve this answer | |
  • I have updated the code, still it is not working. – Jancy Abraham Sep 22 '17 at 10:03
  • You are trying to get the registry without setting it!! – Pavan Kumar Sep 22 '17 at 10:09
  • I have set the registry in the observer . – Jancy Abraham Sep 22 '17 at 10:11
  • I cant see any setRegistry in observer. see my updated answer!! – Pavan Kumar Sep 22 '17 at 10:12
  • This is my code $this->_registry->register('slct_options', $observer->getRequest()->getParams()); Please check the RestrictAddToCart class – Jancy Abraham Sep 22 '17 at 10:13
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

| improve this answer | |
  • I have updated the code, still it is not working. – Jancy Abraham Sep 22 '17 at 10:03
0

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.

| improve this answer | |
  • I have updated the code, still it is not working. – Jancy Abraham Sep 22 '17 at 10:03

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.