magento-lts icon indicating copy to clipboard operation
magento-lts copied to clipboard

Remove exceeding shipping quotes in cart

Open rafaelpatro opened this issue 6 years ago • 12 comments

Hi all! I would like to suggest removing this piece of code: https://github.com/OpenMage/magento-lts/blob/9f9a99ba397e00c7471b2234ef1e61cc48d600f7/app/code/core/Mage/Checkout/Model/Cart.php#L462

Today cart index action are requesting shipping quotes every time. Decreasing page load and user experience. I think this flag should respect the user action. This happens only in estimatePostAction. https://github.com/OpenMage/magento-lts/blob/9f9a99ba397e00c7471b2234ef1e61cc48d600f7/app/code/core/Mage/Checkout/controllers/CartController.php#L552

So user could remove and update the cart without collecting totals in every action.

rafaelpatro avatar May 30 '19 17:05 rafaelpatro

Today cart index action are requesting shipping quotes every time.

Are you sure this is happening on a clean Magento installation? This code has been there since 1.1.1 and I'm pretty sure it hasn't always requested shipping quotes every time you visit the cart.

colinmollenhour avatar May 31 '19 18:05 colinmollenhour

We have had this issue for many years. We worked around it by adding a line in /app/code/core/Mage/Checkout/controllers/CartController.php

added right bbfore line 233 $cart->addProduct($product, $params); $cart->getQuote()->removeAllAddresses()->getShippingAddress()->setPostcode('')->setCollectShippingRates(false)->removeAllShippingRates();

Effectively removes the customers shipping postalcode from the checkout and clears the collectShippingRates flag when adding additional items to cart.

discountscott avatar Jul 06 '20 19:07 discountscott

Can somebody elaborate me the details of the problem? And why would it be safe to remove the line?

tmotyl avatar Jul 06 '20 20:07 tmotyl

We have not tested the previous proposed fix. We resolved by adding a line to the cart controller action.

The issue is as follows:

  • Add item to cart
  • Get shipping quote
  • Leave cart and add another item
  • 'Add to cart' appears to take a long time depending on shipping methods
    • Shipping quote gets run again during the add to cart action
    • We have customers that as a result click the add to cart again, causing even more delays (and incorrect qty in cart)

The resolution is to clear the shipping quote on 'add to cart'.

discountscott avatar Jul 06 '20 20:07 discountscott

Perfectly described! Thanks A long time problem in Magento core. Since its construction.

rafaelpatro avatar Jul 06 '20 22:07 rafaelpatro

  • Get shipping quote

what do you mean by that?

tmotyl avatar Jul 07 '20 07:07 tmotyl

Estimate shipping with external APIs, webservices, that requires some time (5-10 seconds). The problem is that every Cart request dispatch new shipping estimation.

rafaelpatro avatar Jul 07 '20 13:07 rafaelpatro

Hi We're about to mess with a crucial part of the system, so we need to be extra careful not to break stuff. I still don't get the whole picture from the issue description nor from the PR. This problem/change needs proper problem analisys before it get fixed.

Do I get it right that the issue doesn't exists on vanilla/fresh Magento?

Estimate shipping with external APIs, webservices, that requires some time (5-10 seconds). The problem is that every Cart request dispatch new shipping estimation.

What about API's that must be called on every cart change to return shipping price based on total cart value or items weight? Do I get it right, that this change will make these implementation broken? I don't know how your API client implementation looks like, but I would expect that you can avoid calling your API when not necessary (by keeping the result untill meaningful changes are made to the quote).

tmotyl avatar Jul 07 '20 22:07 tmotyl

We're about to mess with a crucial part of the system, so we need to be extra careful not to break stuff.

Totally agree 👍

Do I get it right that the issue doesn't exists on vanilla/fresh Magento?

You're able to reproduce this issue in any Magento CE 1.9. But you won't see a delay if not using a shipping integration (API, webservice or something else).

I don't know how your API client implementation looks like, but I would expect that you can avoid calling your API when not necessary (by keeping the result untill meaningful changes are made to the quote).

That is the point. You cannot avoid it. Customer can add 2 or 3 or 20 products without going to cart. Each action makes meaningful changes, so requesting new shipping calculations. So each Add action can lead 10 seconds.

Shipping rates should be cleared after meaningful changes in cart. And updated only when requested.

rafaelpatro avatar Jul 07 '20 23:07 rafaelpatro

Has anyone thought of a good mechanism to solve this? I tried previously, but the implementation was not ideal...

discountscott avatar Nov 01 '23 21:11 discountscott

somebody willing to provide a PR for this?

fballiano avatar May 08 '24 13:05 fballiano

we solved internally (not ideally), with the following. It basically just removes the quote before additional items get added to the cart.

We'd create a PR to solve this problem, but I doubt everyone wants this as the solution... ?

class DiscountFence_ShippingQuote_Model_Observer{
	public function invalidateShippingQuote($observer){
		$quote           = Mage::getSingleton('checkout/session')->getQuote();
		if($quote->hasItems()) {
			$shippingAddress = $quote->getShippingAddress();

			if ($shippingAddress) {
				$quote->removeAllAddresses()->getShippingAddress()->setPostcode('')->setCollectShippingRates(false)->removeAllShippingRates();
				$quote->setTotalsCollectedFlag(false)->collectTotals();
			}
		}
	}
}

that observer is on checkout_cart_product_add_before, checkout_cart_update_items_before, and sales_quote_remove_item

discountscott avatar May 08 '24 13:05 discountscott