3

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?

3

This is what I did:

In Magento\Quote\Model\QuoteManagement at line 550 the 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.

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

Solutions for Magento 2.3.5. 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;
    }
}
1
  • It's creating a quote id with null reserved order id and also redirecting to checkout cart page but item is removing from cart. – Sarvesh Tiwari Oct 21 '20 at 13:40
1

For magento 2 please try and inject \Magento\Checkout\Model\Session $checkoutSession to _construct and write...

$this->checkoutSession->restoreQuote();

...in your Failure.php

0

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());
            $quote->setIsActive(1)->setReservedOrderId(null);
            $this->quoteRepository->save($quote);
            $this->replaceQuote($quote)->unsLastRealOrderId();
            $this->_eventManager->dispatch('restore_quote', ['order' => $order, 'quote' => $quote]);
            return true;
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
        }
    }
    return false;
}
6
  • 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
0

Below is the solutions for Magento 2.3.5. 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, privacy policy and cookie policy

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