2

How can I stop Magento to empty cart after the customer fails to pay with a card?

The customer can use a different card but the cart already becomes empty and he/she needs to put all the items back into card again which makes the site loose customer.

  • In Magneto 1.x there is no out of the box solution. I did this on a custom payment method by getting the latest quote and repopulating the cart. If you are interested I can share my code – Yiorgos Moschovitis Dec 10 '15 at 7:01
  • That will be perfect if you can share your code @Yiorgos Moschovitis – tufanayd Dec 10 '15 at 7:08
2

In your payment method's controller you'll have an action for failed/rejected/canceled orders.

Let's say it's called failAction(). Then you can add something like the following to your function.

public function failAction() {
...
    if(Mage::getSingleton('checkout/session')->getLastRealOrderId()){
        if ($lastQuoteId = Mage::getSingleton('checkout/session')->getLastQuoteId()){
            $quote = Mage::getModel('sales/quote')->load($lastQuoteId);
            $quote->setIsActive(true)->save();
        }
        Mage::getSingleton('core/session')->addError(Mage::helper('module_name')->__('Inform the customer for failed transaction'));
        $this->_redirect('checkout/cart'); //Redirect to cart
        return;
    }
...
 }
  • sorry, it was not helped to me. – Teja Bhagavan Kollepara Jul 20 '16 at 11:37
  • It worked, but in my failure screen, after running your code, when I check Mage::helper('checkout/cart')->getCart() it says it's empty, and after moving to other page then the cart is loaded again... – nano Aug 19 '16 at 12:14
  • I'm not redirecting users to 'checkout/cart' as you suggest, I'm loading the failure screen, and there, my minicart says there are no items. When moving to other page everything works fine – nano Aug 19 '16 at 12:38
1

/app/code/core/Mage/Checkout/controllers/OnepageController.php this file is the actual controller file, but depends up on the payment method extensions it will change with Namespace/Modulename/Checkout/controllers/OnepageController.php

Find function saveOrderAction()

find these lines

$this->getOnepage()->getQuote()->save();
        /**
         * when there is redirect to third party, we don't want to save order yet.
         * we will save the order in return action.
         */
        if (isset($redirectUrl)) {
            $result['redirect'] = $redirectUrl;
        }

        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

comment this line //$this->getOnepage()->getQuote()->save(); and add below codes inside the if condition so the condition will look like ..

    if (isset($redirectUrl)) {
                $result['redirect'] = $redirectUrl;
                $this->getOnepage()->getQuote()->setIsActive(1) ;
            }
    $this->getOnepage()->getQuote()->save();
   $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

This is i have done with the third party Payment extension.

  • This solution is creating the order before trying the payment. But if you cancel several times and then go to MyAccount/MyOrders you'll see all the tries, all the orders. I just want to keep the quote but not the orders. Didn't work for me.. – nano Aug 12 '16 at 10:37
  • It was worked for me when i am using default checkout in my magento theme. but when i used IWD onepage checkout, it is not worked. – Teja Bhagavan Kollepara Aug 17 '16 at 7:19
  • May be iwd onepage is using some other function, i haven't checked with this – Naveenbos Aug 18 '16 at 12:04
0

For Naveen answer, if any one using IWD extension, then function saveOrderAction() is in app/code/community/IWD/Opc/controllers/JsonController.php

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.