title: Update magento cart total before saving order
body: <p>I need to remove some products from cart after the customer press the "Place order" button. I can remove the products with a custom event and observer and update the cart products, but i cant update the cart total. 
  In the observer i try to update the price with: </p>
  
  <pre><code>    $granTotal = $cart-&gt;getGrandTotal() - $cart-&gt;getShippingAmount();
      $cart-&gt;setGrandTotal($GranTotal - $sum)-&gt;save();
  
      $baseGranTotal = $cart-&gt;getBaseGrandTotal() - $cart-&gt;getShippingAmount();
      $cart-&gt;setBaseGrandTotal($baseGranTotal - $sum)-&gt;save();
  
      $cart-&gt;setTotalsCollectedFlag(false)-&gt;collectTotals();
      $cart-&gt;save();
  </code></pre>
  
  <p>But the total of the cart don't change. I find that the total is stored in the cart address too and used to set the cart total in Mage_Sales_Model_Quote collectTotals() function.
  Here:</p>
  
  <pre><code>$this-&gt;setSubtotal((float) $this-&gt;getSubtotal() + $address-&gt;getSubtotal());
  $this-&gt;setBaseSubtotal((float) $this-&gt;getBaseSubtotal() + $address-&gt;getBaseSubtotal());
  </code></pre>
  
  <p>Then i tried to change the address total before calling the collectTotals(), but the cart total still don't change.</p>
  
  <p>My custom event is triggered at the beginning in Mage_Checkout_Model_Type_Onepage saveOrder() function.</p>
  
  <p>I am running out of ideas of how to update the total of the cart.
  My observer looks like this:</p>
  
  <pre><code>public function changeCart(){
      $cart = Mage::getSingleton('checkout/session')-&gt;getQuote();
      $data = array();
      $inquiry = array();
      $sum = 0;
      //remove out of stock items to save them in custom model
      foreach ($cart-&gt;getAllItems() as $item) {
          if($item-&gt;getProduct()-&gt;getData('is_in_stock') == 0){
              $cart-&gt;removeItem($item-&gt;getItemId())-&gt;save();
              $inquiry[] = $item-&gt;getProduct();
              $data['qty'][$item-&gt;getProduct()-&gt;getId()] = $item-&gt;getData('qty');
              $sum += $item-&gt;getProduct()-&gt;getFinalPrice();
              $dobavljivost = $item-&gt;getProduct()-&gt;getResource()-&gt;getAttributeRawValue($item-&gt;getProduct()-&gt;getId(), 'dobavljivost', Mage::app()-&gt;getStore());
              Mage::getSingleton('core/session')-&gt;setDobavljivost($dobavljivost);
          }
      }
  
      foreach($cart-&gt;getAllAddresses() as $a){
          $a-&gt;setGrandTotal(0);;
          $a-&gt;setBaseGrandTotal(0);
          $a-&gt;setData('subtotal', 0);
          $a-&gt;setData('base_subtotal', 0);
      }
  
      $granTotal = $cart-&gt;getGrandTotal() - $cart-&gt;getShippingAmount();
      $cart-&gt;setGrandTotal($GranTotal - $sum)-&gt;save();
  
      $baseGranTotal = $cart-&gt;getBaseGrandTotal() - $cart-&gt;getShippingAmount();
      $cart-&gt;setBaseGrandTotal($baseGranTotal - $sum)-&gt;save();
  
      $cart-&gt;setTotalsCollectedFlag(false)-&gt;collectTotals();
      $cart-&gt;save();
  }
  </code></pre>
  
  <p>Thank you for any help!</p>