1

I have below code of observer

namespace Custom\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class TestObserver implements ObserverInterface {

    protected $_urlManager;
    protected $_checkoutSession;
    protected $_cart;
    protected $_messageManager;
    protected $_redirect;
    protected $_request;
    protected $_response;
    protected $_responseFactory;
    protected $_resultFactory;
    protected $_scopeConfig;
    protected $_product;

    public function __construct(\Magento\Framework\UrlInterface $urlManager, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Framework\App\Response\RedirectInterface $redirect, \Magento\Checkout\Model\Cart $cart, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Framework\App\RequestInterface $request, \Magento\Framework\App\ResponseInterface $response, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Catalog\Model\Product $product, \Magento\Framework\App\ResponseFactory $responseFactory, \Magento\Framework\Controller\ResultFactory $resultFactory
    ) {
        $this->_urlManager = $urlManager;
        $this->_checkoutSession = $checkoutSession;
        $this->_redirect = $redirect;
        $this->_cart = $cart;
        $this->_messageManager = $messageManager;
        $this->_request = $request;
        $this->_response = $response;
        $this->_responseFactory = $responseFactory;
        $this->_resultFactory = $resultFactory;
        $this->_scopeConfig = $scopeConfig;
        $this->_product = $product;
    }

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

       // $this->_redirect->redirect($this->_response, $this->_urlManager->getUrl('checkout'));
       // $resultRedirect = $this->_resultFactory->create(ResultFactory::TYPE_REDIRECT);
       // $resultRedirect->setUrl($this->_urlManager->getUrl('checkout'));
       $this->_responseFactory->create()->setRedirect($this->_urlManager->getUrl('checkout'))->sendResponse();
        // $this->_response->setRedirect($this->_urlManager->getUrl('checkout'));
         $this->_checkoutSession->setNoCartRedirect(true); // Redirect to Checkout Page
        return $this;
    }
}

Have tried with $this->_checkoutSession->setNoCartRedirect(true); & $this->_checkoutSession->setNoCartRedirect(false);

Other classes used for my purpose.

Pages loads on Console (Firebug). But not redirecting actually on checkout page.

5
  • try this getUrl('checkout/index/index') Feb 22, 2017 at 9:16
  • Which event you are using ?
    – Keyur Shah
    Feb 22, 2017 at 9:17
  • Using checkout_cart_product_add_after event @KeyurShah
    – Jackson
    Feb 22, 2017 at 9:19
  • getUrl('checkout/index/index') only @BilalUsean or $this->?
    – Jackson
    Feb 22, 2017 at 9:21
  • I don't see any fault in your code may be some where else magento hard code like need to mention like route/controller/action -> checkout/index/index that's why I suggest Feb 22, 2017 at 9:24

1 Answer 1

1

You need to use code which is following as:

use Magento\Framework\Event\ObserverInterface;

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

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

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $cartUrl = $this->_url->getUrl('checkout/cart/index');
        $this->_responseFactory->create()->setRedirect($cartUrl)->sendResponse();            
        exit;
    }
}
9

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.