Magento - save quote_address on cart addAction using observer
- by Byron
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.
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).
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.
The observer method's I've used are:
checkout_cart_update_item_complete
checkout_cart_product_add_after
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()
$quote->getShippingAddress()
->setCountryId($params['estimate_to_country'])
->setCity($params['estimate_to_city'])
->setPostcode($params['estimate_to_zip_code'])
->setRegionId($params['estimate_to_state_code'])
->setRegion($params['estimate_to_state'])
->setCollectShippingRates(true)
->setShippingMethod($params['carrier_method'])
->setQuote($quote)
->save();
$quote->getBillingAddress()
->setCountryId($params['estimate_to_country'])
->setCity($params['estimate_to_city'])
->setPostcode($params['estimate_to_zip_code'])
->setRegionId($params['estimate_to_state_code'])
->setRegion($params['estimate_to_state'])
->setCollectShippingRates(true)
->setShippingMethod($params['carrier_method'])
->setQuote($quote)
->save();
$quote->save();
$cart->save();
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.
Thanks so much, let me know if I need to clarify