3

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?

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.

1

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.

  • what happen to the saved order? – Doni Wibowo Sep 11 '17 at 9:48
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));
  • 1
    Thanks for your answer, but I need Magento 2 code. Mage::helper is for magento 1.x only – Billy VG Mar 15 '17 at 6:10
  • @BillyVG, hi. oh ok sorry. i dont know about magento-2. i thought it may help. dont mind. – Teja Bhagavan Kollepara Mar 15 '17 at 9:30
0

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

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.