1

I'm using two different events controller pre dispatch and controller post dispatch.I have set customvalue using catalogsession in predispatch event and try to get the value in post dispatch event.but the session value returns null.If I try to get value in predispatch event.It returns correctly.

predispatch observer

protected $registry;

protected $request;

protected $catalogSession;

protected $filterValue;

protected $context;

public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\App\Request\Http $request,
    \Magento\Catalog\Model\Session $catalogSession,
    Registry $registry
) {
    $this->productRepository = $productRepository;
    $this->registry = $registry;
    $this->productFactory = $productFactory;
    $this->context = $context;
    $this->request = $request;
    $this->catalogSession = $catalogSession;
    
}



public function execute(\Magento\Framework\Event\Observer $observer)
{
    $full_action_name = $this->request->getFullActionName();

    if ($full_action_name != 'catalog_category_view') {
        return;
    }
    $param = $this->request->getParams();
    unset($param['mAjax']);
    $ab =$this->catalogSession->setFilter($param);
   var_dump($ab); 
   return $this;
    
   
  

post dispatch observer

protected $request;

private $registry;

protected $context;

protected $catalogSession;

protected $FilterCookie;

public function __construct(
    Registry $registry,
    \Magento\Framework\App\Request\Http $request,
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\Session $catalogSession,
    
)
{    $this->request = $request;
    $this->registry = $registry;
    $this->catalogSession = $catalogSession;
    $this->context = $context;
  
    
}

/**
 *
 * @param \Magento\Framework\Event\Observer $observer
 * @return void
 */
public function execute(\Magento\Framework\Event\Observer $observer)
{
    /** @var \Magento\Framework\App\Action\Action $controller */
    $full_action_name = $this->request->getFullActionName();
    
    //$layout = $observer->getEvent()->getLayout();
    if ($full_action_name != 'catalog_category_view') {
        return;
    }
   $a =  $this->catalogSession->getFilter();
 var_dump($a); exit();
    
2
  • Could you please provide the code?
    – Joao71
    Jul 9, 2020 at 10:54
  • @Joao71 updated my observer Jul 9, 2020 at 11:36

1 Answer 1

0

The parameter you are trying to store is sent in the $request object. You can get it on the post dispatch.

if you want you can store the value in a custom variable to make sure it does not conflic with other variable sin the HTTP Header.

Example:

predispatch observer

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $full_action_name = $this->request->getFullActionName();

    if ($full_action_name != 'catalog_category_view') {
        return;
    }
    $param = $this->request->getParams();
    unset($param['mAjax']);
   // $ab =$this->catalogSession->setFilter($param);
   $this->request->setPostValue('test_param', $param);
   var_dump($ab); 
   return $this;
}

postdispatch observer

public function execute(\Magento\Framework\Event\Observer $observer)
{
    /** @var \Magento\Framework\App\Action\Action $controller */
    $full_action_name = $this->request->getFullActionName();
    
    //$layout = $observer->getEvent()->getLayout();
    if ($full_action_name != 'catalog_category_view') {
        return;
    }
   //$a =  $this->catalogSession->getFilter();
     $test_param = $this->request->getPostValue('test_param');
 var_dump($a); exit();
}

let me know if you need any further assistance and don't forget to mark as answer if this solved your problem :)

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.