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.

2
  • 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 Dec 10, 2015 at 7:01
  • That will be perfect if you can share your code @Yiorgos Moschovitis
    – tufanayd
    Dec 10, 2015 at 7:08

4 Answers 4

3

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;
    }
...
 }
3
  • sorry, it was not helped to me. Jul 20, 2016 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, 2016 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, 2016 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.

3
  • 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, 2016 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. Aug 17, 2016 at 7:19
  • May be iwd onepage is using some other function, i haven't checked with this
    – Naveenbos
    Aug 18, 2016 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

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;
    }
}
1
  • It's not working at all why everyone is replying the same answer while it's not working. Oct 22, 2020 at 7:27

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.