4

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

5 Answers 5

2

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.

1
  • what happen to the saved order? Commented Sep 11, 2017 at 9:48
1

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.

0

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));
2
  • 1
    Thanks for your answer, but I need Magento 2 code. Mage::helper is for magento 1.x only
    – Billy VG
    Commented Mar 15, 2017 at 6:10
  • @BillyVG, hi. oh ok sorry. i dont know about magento-2. i thought it may help. dont mind. Commented Mar 15, 2017 at 9:30
0

This code works fine in Magento 2.3, $this->checkoutSession->restoreQuote(); and inject \Magento\Checkout\Model\Session $checkoutSession to _construct.

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 and acknowledge you have read our privacy policy.

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