2

Shopping cart becomes empty after cancel the payment. i am using IWD onepage checkout. My website is Depoto-1.9.1.1

            /**
             * @todo remove wishlist observer processAddToCart
             */
            Mage::dispatchEvent('checkout_cart_add_product_complete',
                array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
            );

            if (!$this->_getSession()->getNoCartRedirect(true)) {
                if (!$cart->getQuote()->getHasError()) {
                    $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
                    $this->_getSession()->addSuccess($message);
                }
                $this->_goBack();
            }
        } catch (Mage_Core_Exception $e) {
            if ($this->_getSession()->getUseNotice(true)) {
                $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
            } else {
                $messages = array_unique(explode("\n", $e->getMessage()));
                foreach ($messages as $message) {
                    $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
                }
            }

            $url = $this->_getSession()->getRedirectUrl(true);
            if ($url) {
                $this->getResponse()->setRedirect($url);
            } else {
                $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
            }
        } catch (Exception $e) {
            $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
            Mage::logException($e);
            $this->_goBack();
        }
    }

  • May i know how did solve this? @Teja – Gem Aug 20 at 10:57
3

Yes this is the default feature of the Magento. whenever you place on the Place order button on website it will create order and your cart becomes clear and redirect to the payment method page and their comes two scenario basically.

  1. Your payment success. once your payments gets success payment method will return success message params to the checkout_onepage_controller_success_action and updates order status as success.
  2. Your payment failed or you canceled on the payment gateway page Once you either cancel payment or your payment gets failed then your payment gateway returns to the checkout_onepage_controller_failure_action with response and updates order status.

So in both cases order gets created and your cart gets empty.

So if you still want to keep the products in the cart if your payment gets failed then you can customize the code with following steps

Write the observer checkout_onepage_controller_failure_action event and find the order id and get the product ids from the order and add this products programmatically to cart then its done

  • I know this but i would like to keep the products in shopping cart after customer cancel the payment. for ex: if customer selects payment gateway instead of COD, he has to cancel that right? then he got empty cart. i want to keep them in cart – Teja Bhagavan Kollepara Aug 17 '16 at 7:47
  • from where a customer will cancel his/her order? – Amit Bera Aug 17 '16 at 8:13
  • since you said instead of cod customer selects other payment method and click on place order button then the website is redirected to the payment gateway from here the user can cancel the payment. so once he cancels order on gateway page it will be redirected to the magento site with empty cart. so you can use what i had already methoned in the code – Pradeep Sanku Aug 17 '16 at 9:02
  • @AmitBera, At checkout page, after select the payment method, he is redirecting to payment gateway page. here he cancelled the transaction. then that cart is empty. – Teja Bhagavan Kollepara Aug 17 '16 at 9:51
  • write the observer checkout_onepage_controller_failure_action event and find the order id and get the product ids from the order and add this products programmatically to cart then its done – Pradeep Sanku Aug 17 '16 at 9:53
0

/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.

  • I already did this. it worked in my default check out page. but i am using IWD onepage checkout. here i am facing issue. – Teja Bhagavan Kollepara Aug 19 '16 at 5:07
  • is there anyway to resume checkout after cancelled payment, ex- user directly show payment methods after return from payment site – Suneth Kalhara Apr 17 at 2:56
0

In order to restore an order item to cart,you can use below code:

$cart = Mage::getSingleton('checkout/cart')        
$items = $order->getItemsCollection();
foreach ($items as $item) {
    try {
        $cart->addOrderItem($item);
    } catch (Mage_Core_Exception $e){
           echo $e->getMessage();
    } catch (Exception $e) {
           Mage::helper('checkout')->__('Cannot add the item to shopping cart.');
    }
}

$cart->save();

Assume that you already have Order object

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.