Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Magento from emptying the shopping cart before payment confirmation?

This is one of the most vital problems I have found since I started testing Magento for my web store. It's kind of a no-brainer that it's absolutely unnecessary and harmful to sales to empty cart before payment confirmation, which unfortunately, Magento does.

If the user selects PayPal (website standard) for payment method and for some reason clicks "Back to xxxx" (your business name at PayPal) on the PayPal payment page without paying, PayPal would redirect the user back to http://www.example.com/checkout/cart/, which now is an EMPTY cart.

I think it should be after payment confirmation / PayPal IPN that the cart be empty-ed, instead of any point before that.

Even if the user wants to continue again, he or she'd be annoyed from searching and adding all the products again and would very probably just leave.

Any idea how I can work around this?

like image 219
datasn.io Avatar asked Feb 15 '12 09:02

datasn.io


People also ask

How does the persistent shopping cart option work in Magento 2?

A persistent shopping cart in Magento is almost the same as a regular Magento shopping cart. However, the persistent cart can store products customers added to the cart for a period specified in the configuration, up to one year. The persistent cookie remains active even after the session cookies expire.

How do I restrict add to cart in Magento 2?

Step 1. Log in to the admin panel and navigate to Stores > Settings > Configuration to disable add to cart button in Magento 2. Step 2. In the Catalog tab, click Catalog, open the Category Permissions section, and set Enable to Yes.

What is mini cart in Magento?

The mini cart displays a summary of items in the cart. It is enabled by default, and appears when you click the Cart link at the top of the page. The link can be configured to display the number of different products (or SKUs) in the cart, or the total quantity of all items. Mini Cart.


2 Answers

This worked for me:

File: ~/app/code/core/Mage/Checkout/controllers/OnepageController.php

Replace this:

$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));

With this one:

/**
 * 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->getOnepage()->getQuote()->setIsActive(1) ;
}
$this->getOnepage()->getQuote()->save();
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
like image 66
Jonathan Delgado Avatar answered Sep 22 '22 13:09

Jonathan Delgado


For Paypal I found the cancel action inside the app/code/core/Mage/Paypal/controllers/StandardController.php cancelAction

I changed the code like that for cancel action

public function cancelAction()
{
    $session = Mage::getSingleton('checkout/session');
    $cart = Mage::getSingleton('checkout/cart');
    $session->setQuoteId($session->getPaypalStandardQuoteId(true));
    if ($session->getLastRealOrderId()) {
        $incrementId = $session->getLastRealOrderId();
        if (empty($incrementId)) {
            $session->addError($this->__('Your payment failed, Please try again later'));
            $this->_redirect('checkout/cart');
            return;
        }
        $order = Mage::getModel('sales/order')->loadByIncrementId($session->getLastRealOrderId());
        $session->getQuote()->setIsActive(false)->save();
        $session->clear();
        try {
            $order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_CANCEL, true);
            $order->cancel()->save();
        } catch (Mage_Core_Exception $e) {
            Mage::logException($e);
        }
        $items = $order->getItemsCollection();
        foreach ($items as $item) {
            try {
                $cart->addOrderItem($item);
            } catch (Mage_Core_Exception $e) {
                $session->addError($this->__($e->getMessage()));
                Mage::logException($e);
                continue;
            }
        }
        $cart->save();
        $session->addError($this->__('Your payment failed. Please try again later'));
    }
    $this->_redirect('checkout/cart');
}

It worked pretty good for me and there is no need to change any other place for that.

It marks the current order as Cancelled and restores the cart with using that order and redirects the user to cart again.

like image 30
CntkCtn Avatar answered Sep 19 '22 13:09

CntkCtn