2

In Magento 2.2.5, after payment failure cart is going empty. I want to keep this, that if payment failed cart will not be empty, that will show last cart items.

I'm using the PayUMoney Payment Method. How can I do this?

I get a lot of example of this problem on Magento 1.9 version but didn't get any suitable solution for Magento 2.2.5.

Please help me, how I solve this issue.

Thank you

3 Answers 3

2

This is a sample code for reactivate quote when payment is failed. This is a controller where it redirect after payment failure. You need to use it in your payment module and update the code according to your requirement or module structure.

<?php 
namespace [Vendor]\[Module]\Controller\Order;

class Failure extends \Magento\Framework\App\Action\Action
{
    ...
    protected $_quoteFactory;
    protected $_checkoutSession;
    ...
    public function __construct(
        ...
        \Magento\Quote\Model\QuoteFactory $quoteFactory,
        \Magento\Checkout\Model\Session $checkoutSession,
        ...
    ){
        ...
        $this->_quoteFactory = $quoteFactory;
        $this->_checkoutSession = $checkoutSession;
        ...
    }

    public function execute()
    {
        $order = $this->_checkoutSession->getLastRealOrder();
        $quote = $this->_quoteFactory->create()->loadByIdWithoutStore($order->getQuoteId());
        if ($quote->getId()) {
            $quote->setIsActive(1)->setReservedOrderId(null)->save();
            $this->_checkoutSession->replaceQuote($quote);
            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setPath('checkout/cart');
            $this->messageManager->addWarningMessage('Payment Failed.');
            return $resultRedirect;
        }
    }
}
2
  • Thank you for sharing the solution. But unable to do this. in the file path -- vendor/module-checkout/Controller/Onepage/Failure.php in this page I tried to implement your above code. But it's not working for me. I tried to implement in my payment method extension that is PayUM. But didn't find any failure page. I think this extension using Magento default failure function. Please help, what I did wrong.
    – M.Suman
    Commented Sep 14, 2018 at 6:56
  • I followed the same code in the failure page payment method but it did not work for me. always items are removing from the cart. Commented Oct 22, 2020 at 8:02
1

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;
    }
}
1
  • your code doesn't works at all.
    – Nit
    Commented Oct 26, 2020 at 10:52
0

You can restore your order with

$this->_checkoutSession->restoreOrder()

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.