0

I have below code, which in not working from Category Listing Page.

Example: http://magento2-demo.nexcess.net/gear/bags.html

namespace Custom\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class TestObserver implements ObserverInterface {
    protected $_responseFactory;
    protected $_url;
    protected $_objectManager;

    public function __construct(\Magento\Framework\App\ResponseFactory $responseFactory,\Magento\Framework\UrlInterface $url, \Magento\Framework\ObjectManagerInterface $objectManager)
    {
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
        $this->_objectManager = $objectManager;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $controller = $observer->getControllerAction();
        $response = $observer->getResponse();
        $controllerAction = $observer->getEvent()->getControllerAction();
        $cartUrl = $this->_url->getUrl('checkout/index/index');     
        $observer->getResponse()->setRedirect($cartUrl);
        exit;

        $response = $observer->getEvent()->getData('response');
        $response->setRedirect($cartUrl)->sendResponse();
        exit;
        $observer->getEvent()->getFront()->getResponse()->setRedirect($cartUrl);
        //$observer->getResponse()->setRedirect($cartUrl);
        exit;

        $response = $observer->getEvent()->getResponse();
        $response->setRedirect($cartUrl);
        exit;
    }
}

It gives below error

Fatal error: Call to a member function setRedirect() on null in

In Plugin it works fine Magento 2: How to redirect to checkout page after product added to cart via Observer/Plugin from category page?

How to use same thing in Observer?

5 Answers 5

1

di.xml

<type name="Magento\Customer\Model\Account\Redirect">
    <plugin name="NewAcctRedirectPlug" type="<vendor_name>\<module_name>\Plugin\Redirect" disabled="false" sortOrder="1"/>
</type>

Redirect.php

<?php

namespace <vendor_name>\<module_name>\Plugin;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Registry;
use Magento\Framework\UrlInterface;

class Redirect
{
     protected $coreRegistry;

     protected $url;

     protected $resultFactory;

     public function __construct(Registry $registry, UrlInterface $url, ResultFactory $resultFactory)
     {
         $this->coreRegistry = $registry;
         $this->url = $url;
         $this->resultFactory = $resultFactory;
     }

     public function aroundGetRedirect ($subject, \Closure $proceed)
     {
        /** @var \Magento\Framework\Controller\Result\Redirect $result */
         $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);            
         $result->setUrl($this->url->getUrl('checkout/index/index'));

         return $result;
     }
 }
1
  • hahaaha majak kari lo have nana manso ni @RakeshJesadiya Feb 22, 2018 at 11:47
0

Please use this

public function execute(Observer $observer) {
        $event = $observer->getEvent();

        $customerBeforeAuthUrl = $this->_url->getUrl('contact/index/index');
        $this->_responseFactory->create()->setRedirect($customerBeforeAuthUrl)->sendResponse();
        die();
    }
1
  • It's loading same URL in AJAX but not redirecting :(
    – Jackson
    Mar 8, 2017 at 7:06
0

You can redirect from observer using below way,

    public function __construct(
        \Magento\Framework\App\ResponseInterface $response,   
    ) {
        $this->_response = $response;   
    }

public function execute(\Magento\Framework\Event\Observer $observer) { 
    $response = $observer->getControllerAction()->getResponse();
    $url = 'custom url';
    $response->setRedirect($url);
    $response->sendResponse();
    exit;
}
4
  • It's not giving any error. But it's not redirecting :(
    – Jackson
    Mar 8, 2017 at 7:04
  • Try with updated answer. Mar 8, 2017 at 7:07
  • 1
    Giving error Fatal error: Call to a member function getResponse() on null
    – Jackson
    Mar 8, 2017 at 7:15
  • Is it good to add exit by this way as per coding standard ? Dec 11, 2018 at 10:14
0

Redirection can be done by this, update this code inside execute method

<?php
public function execute(\Magento\Framework\Event\Observer $observer)
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $redirect = $objectManager->create('Magento\Framework\App\Response\RedirectInterfaceFactory')->create();
    $controller = $observer->getControllerAction();
    $redirect->redirect($controller->getResponse(), 'checkout/index/index');
}
8
  • Giving same error Fatal error: Call to a member function getResponse() on null
    – Jackson
    Mar 8, 2017 at 7:05
  • I have updated the answer. If in case, you got that error even with the update one,could let me know which observer you are using? Mar 8, 2017 at 7:09
  • Fatal error: Call to a member function getResponse() on null Error
    – Jackson
    Mar 8, 2017 at 7:16
  • Could you please mention the event/observer you are using? Mar 8, 2017 at 7:24
  • I'm using checkout_cart_product_add_after event. It seems not working on Category page
    – Jackson
    Mar 8, 2017 at 7:25
0

You can redirect from predispatch event observer using below way,

public function __construct(
        \Magento\Framework\App\ActionFlag $actionFlag,   
    ) {
        $this->_actionFlag = $actionFlag;   
    }

public function execute(\Magento\Framework\Event\Observer $observer) { 
    $controller = $observer->getControllerAction();
    $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
    $url = 'custom url';
    $controller->getResponse()->setRedirect($url);
}

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.