magentno 2 I want user to keep its shopping cart after payment failure. Cart becomes empty when cancel the payment transaction in checkout.

How should I change the Failure.php in Magento checkout module?


This is what I did: There's a file in your vendor folder at this location: Magento\Quote\Model\QuoteManagement

At line 550: quote is being made inactive. So I commented out the following code:

// $quote->setIsActive(false);
        // $this->eventManager->dispatch(
        //     'sales_model_service_quote_submit_success',
        //     [
        //         'order' => $order,
        //         'quote' => $quote
        //     ]
        // );
        // $this->quoteRepository->save($quote);

This would also help you to maintain cart state if the user goes back from the payment page back to checkout page.

  • How will this affect the stock quantity of the products in the cart. – Sufyan Khot Jul 2 at 10:53

This depends by the payment method you use... as some of them offer the feature you want. Take a look at Magento\Checkout\Model\Session::restoreQuote() to get an idea about what to do

 * Restore last active quote
 * @return bool True if quote restored successfully, false otherwise
public function restoreQuote()
    /** @var \Magento\Sales\Model\Order $order */
    $order = $this->getLastRealOrder();
    if ($order->getId()) {
        try {
            $quote = $this->quoteRepository->get($order->getQuoteId());
            $this->_eventManager->dispatch('restore_quote', ['order' => $order, 'quote' => $quote]);
            return true;
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
    return false;
  • ok thx for reply ill check this Mr.Raul – Chala Chalapathi Mar 19 '18 at 10:04
  • This is not a copy/paste answer... sorry. It's just an orientation about what your payment method should do, in the case it fails – Raul Sanchez Mar 19 '18 at 10:12
  • oh ok ok thx any exact idea pls – Chala Chalapathi Mar 19 '18 at 10:18
  • Cannot be exact if we can't know what payment method we are talking about ;) – Raul Sanchez Mar 19 '18 at 10:49
  • its PayUIndia_Payu payment method – Chala Chalapathi Mar 19 '18 at 10:51

For magento 2 , try inject \Magento\Checkout\Model\Session $checkoutSession to _construct. and write this code in your Failure.php $this->checkoutSession->restoreQuote();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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