2

I'm working on a custom module that will display shipping estimates on the product page. All of my customers are required to sign up and log in (it's a trade-only website), so I have the customer's default shipping address at my disposal (guaranteed). Therefor, I'm going to provide a shipping estimate based on the default shipping address. So far, the only way I've found to convert the customer/address object into a sales_model_quote_address is as follows:

$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultShipping();
$address = Mage::getModel('customer/address')->load($customerAddressId);
$address_data = array (
        'prefix' => $address->getPrefix(),
        'firstname' => $address->getFirstname(),
        'lastname' => $address->getLastname(),
        'street' => $address->getStreet(),
    'city' => $address->getCity(),
    'region_id' => $address->getRegionId(),
    'region' => $address->getRegion(),
    'postcode' => $address->getPostcode(),
    'country_id' => $address->getCountryId(),
    'telephone' => $address->getTelephone(),
    'email' => $address->getEmail()
);

$quoteShippingAddress = new Mage_Sales_Model_Quote_Address();
$quoteShippingAddress->setData($address_data);

This does work, but it seems a bit hacky/messy. Can anyone suggest a sensible alternative? Thanks in advance.

3 Answers 3

7

I was hunting around for this too, and it looks like the quote/address class has a routine that does what we want, called importCustomerAddress:

$quoteShippingAddress->importCustomerAddress($customer->getDefaultShippingAddress());
$quoteShippingAddress->save();
1
private function _setDefaultShippingAddress($sameAsBilling) 
{

        if ($sameAsBilling) { // Set shipping same as billing
            $address =  Mage::getSingleton('customer/session')->getCustomer()->getDefaultBillingAddress();
        } else { // Use Default Shipping address
            $address =  Mage::getSingleton('customer/session')->getCustomer()->getDefaultShippingAddress();
        }

        if ($address) {
            $data = array(  "address_id"    =>  $address->getId(),
                        "city"          =>  $address->getCity(),
                        "company"       =>  $address->getCompany(),
                        "fax"           =>  $address->getFax(),
                        "firstname"     =>  $address->getFirstname(),
                        "lastname"      =>  $address->getLastname(),

                        "country_id"    =>  $address->getCountryId(),
                        "region"        =>  "",
                        "region_id"     =>  $address->getRegionId(),
                        "same_as_billing"=> $sameAsBilling,
                        "street"        =>  array($address->getStreet(),""),
                        "telephone"     =>  $address->getTelephone());

            // Set Address
            $this->getOnepage()->saveShipping($data, $address->getId());
            $this->_getQuote()->getShippingAddress()->importCustomerAddress($address);
            $this->_getQuote()->getShippingAddress()->setCollectShippingRates(true);
        }
    $this->_getQuote()->collectTotals()->save();
    $this->_getQuote()->getShippingAddress()->setCollectShippingRates(true);

}


 /**
 * @return Mage_Sales_Model_Quote
 */
protected function _getQuote()
{
    return Mage::getSingleton('checkout/cart')->getQuote();
}


 /**
 * Get one page checkout model
 *
 * @return Mage_Checkout_Model_Type_Onepage
 */
public function getOnepage()
{
    return Mage::getSingleton('checkout/type_onepage');
}

I think this will do the job. You can use this on Cart Index Action.

1
  • Thanks for that - it looks like a very useful function. Unfortunately, it doesn't exactly answer my original question (which I perhaps did not explain very well). The code is executed in an AJAX call from the product page in order to generate a shipping quote <em>without</em> adding anything to the user's cart (they might not like my shipping prices!). I notice that your function takes the same approach of just copying over all the attributes of the address, so perhaps this really is the only way to convert one type of address object into another. It still seems a bit messy to me.
    – dan__baker
    Jan 23, 2014 at 11:58
0

Ok, I've managed to answer this one myself. It's actually pretty obvious:

$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultShipping();
$customer_address = Mage::getModel('customer/address')->load($customerAddressId);
$quoteShippingAddress = new Mage_Sales_Model_Quote_Address();
$quoteShippingAddress->setData($customer_address->getData()); //pull the data from one and put it in the other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.