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
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;
}
}
}
-
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– M.Suman2018年09月14日 06:56:52 +00:00Commented 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.Sarvesh Tiwari– Sarvesh Tiwari2020年10月22日 08:02:03 +00:00Commented Oct 22, 2020 at 8:02
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 code doesn't works at all.Nit– Nit2020年10月26日 10:52:59 +00:00Commented Oct 26, 2020 at 10:52
You can restore your order with
$this->_checkoutSession->restoreOrder()