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?
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?
You can not prevent a cart from being empty as the cart is converted into order. You can however set the cart as active again in your failure function.
In your failure function, try adding this code -:
if(Mage::getSingleton('checkout/session')->getLastRealOrderId())
{
$lastQuoteId = Mage::getSingleton('checkout/session')->getLastQuoteId());
$quote = Mage::getModel('sales/quote')>load($lastQuoteId);
$quote->setIsActive(true)->save();
}
Above code turns the previous cart/quote as active again.
Whenever any order place cart is converted to order and that cart will inactive, so once the order place new cart is created, so in your case order was already placed and then payment failed. So when user check thier cart its already a new cart, so no items is showing.
I think whenever payment fail you can redirect user to custom page where you can show "Reorder" button, which is same we can see on order detail page on front-end in order history. So when user click on Reorder they will get all the last order items in their cart.
Using below code you can get Reorder link in Magento 1 for Magento2 syntax will change :
$this->getUrl('sales/order/reorder', array('order_id' =>Mage::getSingleton('checkout/session')->getLastOrderId()));
Hope this process help you.
Please open app/code/core/Mage/Checkout/controllers/OnepageController.php
Search for
$this->getOnepage()->getQuote()->save();
//this makes the cart empty (sets the quote as converted to order)
if (isset($redirectUrl)) {
$result['redirect'] = $redirectUrl;
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
And replace this with the following.
if (isset($redirectUrl)) {
$result['redirect'] = $redirectUrl;
$this->getOnepage()->getQuote()->setIsActive(1);
}
$this->getOnepage()->getQuote()->save();
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
This code works fine in Magento 2.3, $this->checkoutSession->restoreQuote(); and inject \Magento\Checkout\Model\Session $checkoutSession to _construct.
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;
}
}