2

I am using a custom module, where I want to redirect to checkout page once a product is adding to cart. For this I have created an event on frontend "checkout_cart_add_product_complete".

For simple product its working fine. But where I am adding a configurable product it will redirect to checkout page with attribute data so(Check in last for URL reference), I am getting an error of noroute on checkout.

Here is my Observer code:

   $observer->getRequest()->setParam('return_url', $this->_url->getUrl('checkout'));

My error on checkout page because url become like:- www.yourDomain.com/checkout/#size=173&color=53

Note: Using Magento 2.3

2
  • please edit the question with the error Feb 13, 2019 at 9:57
  • getting noroute error on checkout. Because my URL become like www.yourDomain.com/checkout/#size=173&color=53 so getting noroute error. It should be www.yourDomain.com/checkout/. Feb 13, 2019 at 10:01

5 Answers 5

2

Try below code if it helps:

<?php
namespace [Vendor]\[modulename]\Observer;

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

class [YourClass] implements ObserverInterface
{
    /**
     * @var \Magento\Framework\App\ResponseFactory
     */
    private $responseFactory;

    /**
     * @var \Magento\Framework\UrlInterface
     */
    private $url;

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

    public function execute(Observer $observer)
    {
        //your custom code
        $redirectionUrl = $this->url->getUrl('checkout/index/index');
        $this->responseFactory->create()->setRedirect($redirectionUrl)->sendResponse();

        return $this;
    }
}
2
  • Not working. Product only add to cart. Not redirect to checkout. Feb 13, 2019 at 12:27
  • $observer->getRequest()->setParam('return_url', $this->_url->getUrl('checkout')); This works but same issue in url. Feb 13, 2019 at 12:27
0

try this

 $requst = $observer->getRequest();
 $params = $requst ->getParams();
 foreach($params as $key => $value) {   
   unset($params[$key]);           
 }
 $request->setParam('return_url', $this->_url->getUrl('checkout')); 
1
  • Not working. A product can not be adding to cart. Feb 13, 2019 at 12:28
0

instead of create observer try to override following controller. Magento\Checkout\Controller\Cart\Add

In Add.php you will find code for redirect on cart page. Change this code with following code

$baseUrl = $this->_objectManager->get('\Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl();

return $this->goBack($baseUrl.'checkout', $product);

I hope this will help you.

0

Hi aniket you can try this

/**
 * @var \Magento\Framework\App\Response\RedirectInterface
 */
protected $redirect;

/**
 * @param \Magento\Framework\App\Response\RedirectInterface $redirect
 */
public function __construct(
    \Magento\Framework\App\Response\RedirectInterface $redirect
) {
    $this->redirect = $redirect;
}


public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $controller = $observer->getControllerAction();
        $this->redirect->redirect($controller->getResponse(), 'checkout/index/index');
OR
$controller->getResponse()->setRedirect($url);
        return $this;
    }
0

This magento 2.3 version issue. If you try with normal checkout and enable option redirect to checkout after product add to cart Enable you will get same error.

To sort Issue: app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js

if (eventData.redirectParameters.length > 0) {
                        parameters = res.backUrl.split('#');
                        //parameters.push(eventData.redirectParameters.join('&'));
                        res.backUrl = parameters.join('#');
                    }

comment Previous Line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.