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.
checkout_cart_product_add_after
event @KeyurShahgetUrl('checkout/index/index')
only @BilalUsean or$this->
?