Skip to main content

You need to useduse code like this.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;
    }
}

You need to used code like this.

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;
    }
}

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;
    }
}
Source Link
Dhiren Vasoya
  • 9.5k
  • 12
  • 35
  • 59

You need to used code like this.

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;
    }
}