title: Magento - save quote_address on cart addAction using observer
body: <p>I am writing a custom Magento module that gives rate quotes (based on an external API) on the product page. After I get the response on the product page, it adds some of the info from the response into the product view's form.</p>
  
  <p>The goal is to save the address (as well as some other things, but those can be in the session for now). The address needs to be saved to the quote, so that the checkout (onestepcheckout, free version) will automatically fill those values in (city, state, zip, country, shipping method [of which there are 3]) and load the rate quote via ajax (which it's already doing).</p>
  
  <p>I have gone about this by using an Observer, and watching for the cart events. I fill in the address and save it. When I end up on the cart page or checkout page ~4/5 times the data is lost, and the sql table shows that the quote_address is getting save with no address info, even though there is an explicit save.</p>
  
  <p>The observer method's I've used are:</p>
  
  <pre><code>checkout_cart_update_item_complete
  checkout_cart_product_add_after
  </code></pre>
  
  <p>The code saving is: (I've tried this with the address, quote and cart all not calling save() with the same results, as well as not calling setQuote()</p>
  
  <pre><code>// $params = Mage::app()-&gt;getRequest()-&gt;getParams()
  // $quote = Mage::getSingleton('checkout/cart')-&gt;getQuote()
  // or
  // $quote = observer-&gt;getProduct()-&gt;getQuoteItem()-&gt;getQuote();
  // where applicable, but both methods seemed to === each other
  $quote-&gt;getShippingAddress()
      -&gt;setCountryId($params['estimate_to_country'])
      -&gt;setCity($params['estimate_to_city'])
      -&gt;setPostcode($params['estimate_to_zip_code'])
      -&gt;setRegionId($params['estimate_to_state_code'])
      -&gt;setRegion($params['estimate_to_state'])
      -&gt;setCollectShippingRates(true)
      -&gt;setShippingMethod($params['carrier_method'])
      -&gt;setQuote($quote)
      -&gt;save();
  
  $quote-&gt;getBillingAddress()
      -&gt;setCountryId($params['estimate_to_country'])
      -&gt;setCity($params['estimate_to_city'])
      -&gt;setPostcode($params['estimate_to_zip_code'])
      -&gt;setRegionId($params['estimate_to_state_code'])
      -&gt;setRegion($params['estimate_to_state'])
      -&gt;setCollectShippingRates(true)
      -&gt;setShippingMethod($params['carrier_method'])
      -&gt;setQuote($quote)
      -&gt;save();
  
  $quote-&gt;save();
  $cart-&gt;save();
  // I also tried:
  Mage::getSingleton('checkout/session')-&gt;setQuote($quote);
  </code></pre>
  
  <p>I imagine there's a good chance this is not the best place to save this info, but I am not quite sure. I was wondering if there is a good place to do this without overriding a whole controller to save the address on the add to cart action.</p>
  
  <p>Thanks so much, let me know if I need to clarify</p>