6

How to prevent Magento 2.0 from emptying cart after failed payments?

I could find multiple solutions for this problem in Magento 1.X but could not find anything for 2.0

I am sure this must be a common issue with many Mangento 2.0 implementors.

Will be greatfull, if someone can suggest some solution for this problem.

2
  • 4
    This is not a common issue. Payment failure should never result in the cart emptying. That would only happen if the order completes and saves successfully, but something interrupts Magento from redirecting to success. If this is happening, something is wrong with your code. What payment method? Any custom code on order submit?
    – Ryan Hoerr
    May 16, 2016 at 19:40
  • As @RyanH. stated, make sure the redirect isn't being interrupted after the order takes place. Check the admin panel to see if the orders are actually coming through or not. Jul 20, 2016 at 0:08

2 Answers 2

0

I know in magento-1.9 version. sorry for this, if this is helps to you. try with following.

/app/code/core/Mage/Checkout/controllers/OnepageController.php this file is the actual controller file, but depends up on the payment method extensions it will change with Namespace/Modulename/Checkout/controllers/OnepageController.php

Find function saveOrderAction()

find these lines

$this->getOnepage()->getQuote()->save();
        /**
         * when there is redirect to third party, we don't want to save order yet.
         * we will save the order in return action.
         */
        if (isset($redirectUrl)) {
            $result['redirect'] = $redirectUrl;
        }

    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

comment this line //$this->getOnepage()->getQuote()->save(); and add below codes inside the if condition so the condition will look like ..

if (isset($redirectUrl)) {
            $result['redirect'] = $redirectUrl;
            $this->getOnepage()->getQuote()->setIsActive(1) ;
        }
    $this->getOnepage()->getQuote()->save();
   $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

This is i have done with the third party Payment extension.

0

For Magento 2.3.5, Below is the solution. Please override Failure.php(Core file path: /vendor/magento/module-checkout/Controller/Onepage) and change public function as below

public function execute()
{
    
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $_checkoutSession = $objectManager->create('\Magento\Checkout\Model\Session');
    $_quoteFactory = $objectManager->create('\Magento\Quote\Model\QuoteFactory');
    
    $order = $_checkoutSession->getLastRealOrder();
    $quote = $_quoteFactory->create()->loadByIdWithoutStore($order->getQuoteId());
    if ($quote->getId()) {
        $quote->setIsActive(1)->setReservedOrderId(null)->save();
        $_checkoutSession->replaceQuote($quote);
        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath('checkout/cart');
        //$this->messageManager->addWarningMessage('Payment Failed.');
        return $resultRedirect;
    }
}

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.