2

How to prevent Magento 2.1 from emptying cart after canceled or failed payments?

When I cancel a payment the cart is empty, and I need to prevent this behaviour.

There are a lot of solutions for 1.9, but I've found nothing for magento2, I've seen even answers about solutions in magento 1.9 for questions about magento2, but not a solution for magento2.

Is there any solution for magento 2.1?

2
  • Hi. Did you find something useful?
    – spiilmusic
    Aug 14, 2017 at 8:31
  • not yet, still clearing cart when cancelling a payment.
    – Alex
    Aug 14, 2017 at 8:47

1 Answer 1

1

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 you have read our privacy policy.

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