1
votes

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()

// $params = Mage::app()->getRequest()->getParams()
// $quote = Mage::getSingleton('checkout/cart')->getQuote()
// or
// $quote = observer->getProduct()->getQuoteItem()->getQuote();
// where applicable, but both methods seemed to === each other
$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 also tried:
Mage::getSingleton('checkout/session')->setQuote($quote);

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

1
That was certainly complex, but you did a fair job outlining it. Welcome!New Alexandria
Thanks! I wanted to give a good overview of what I was trying to do, so there is a lot of explanation. I am an avid user of stack overflow, so I hope I did this ok.Byron

1 Answers

0
votes

In Magento you can create your own events wherever you need but in this case you can use checkout_cart_product_add_after event to update the quore address details.

So here is the code for the same

$quote = Mage::getSingleton('checkout/session')->getQuote();

$billingAddress = Mage::getModel('sales/quote_address')
        ->setStoreId($storeId)
        ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
        ->setCustomerId(('Your_Value_Here'))
        ->setCustomerAddressIpd(('Your_Value_Here'))
        ->setCustomer_address_id(('Your_Value_Here'))
        ->setPrefix(('Your_Value_Here'))
        ->setFirstname(('Your_Value_Here'))
        ->setMiddlename(('Your_Value_Here'))
        ->setLastname(('Your_Value_Here'))
        ->setSuffix(('Your_Value_Here'))
        ->setCompany(('Your_Value_Here'))
        ->setStreet(('Your_Value_Here'))
        ->setCity(('Your_Value_Here'))
        ->setCountry_id(('Your_Value_Here'))
        ->setRegion(('Your_Value_Here'))
        ->setRegion_id(('Your_Value_Here'))
        ->setPostcode(('Your_Value_Here'))
        ->setTelephone(('Your_Value_Here'))
        ->setFax(('Your_Value_Here'));
$quote->setBillingAddress($billingAddress);

$shippingAddress = Mage::getModel('sales/quote_address')
        ->setStoreId($storeId)
        ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
        ->setCustomerId(('Your_Value_Here'))
        ->setCustomerAddressId(('Your_Value_Here'))
        ->setCustomer_address_id(('Your_Value_Here'))
        ->setPrefix(('Your_Value_Here'))
        ->setFirstname(('Your_Value_Here'))
        ->setMiddlename(('Your_Value_Here'))
        ->setLastname(('Your_Value_Here'))
        ->setSuffix(('Your_Value_Here'))
        ->setCompany(('Your_Value_Here'))
        ->setStreet(('Your_Value_Here'))
        ->setCity(('Your_Value_Here'))
        ->setCountry_id(('Your_Value_Here'))
        ->setRegion(('Your_Value_Here'))
        ->setRegion_id(('Your_Value_Here'))
        ->setPostcode(('Your_Value_Here'))
        ->setTelephone(('Your_Value_Here'))
        ->setFax(('Your_Value_Here'));

$quote->setShippingAddress($shippingAddress)
        ->setShipping_method('flatrate_flatrate')
        ->setShippingDescription($this->getCarrierName('flatrate'));


$quote->save();