Search code examples
phpmagento

unset shipping and recalculate totals in magento


Here is my problem:

When I add an E-course (no shipping) and a regular product (has shipping) to my cart and select shipping during checkout the cost gets set in the quote. if I leave checkout after this has been set and then remove the product that has shipping, the shipping cost is still set in the quote and I can't unset it...even by removing all items in the quote.

I'm trying to figure out how to unset the shipping method and recalculate the totals after a product is removed from the cart.


Solution

  • A few things are cached in Quote and Shipping Address model, so you have to do this to make Magento recalculate totals if you change qoute items or shipping:

        $quote->setTotalsCollectedFlag(false);
        $quote->getShippingAddress()->unsetData('cached_items_all');
        $quote->getShippingAddress()->unsetData('cached_items_nominal');
        $quote->getShippingAddress()->unsetData('cached_items_nonnominal');
        $quote->collectTotals();
    

    It worked for me. Magento normally redirects the customer to the cart or to another page after any quote change, so normally it has to calculate totals once per page load.