0

I want to join default shipping address to customer collection.

if shipping address is not available need to join billing address in place of shipping address.

For an example

customer_id email shipping address fields

  1. s@gmail.com shipping info
  2. g@gmail.com shipping info
  3. k@gmail.com billing info

here customer-3 does not contain default shipping address .so i need to replace with default billing address(assumed customer contain default billing address.)

$collection = Mage::getResourceModel('customer/customer_collection')
    ->addNameToSelect()
    ->addAttributeToSelect('email')
    ->addAttributeToSelect('created_at')                
    ->addAttributeToSelect('group_id')
    ->joinAttribute('shipping_prefix', 'customer_address/prefix', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_firstname', 'customer_address/firstname', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_middlename', 'customer_address/middlename', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_lastname', 'customer_address/lastname', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_suffix', 'customer_address/suffix', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_company', 'customer_address/company', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_street', 'customer_address/street', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_postcode', 'customer_address/postcode', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_city', 'customer_address/city', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_country_id', 'customer_address/country_id', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_region', 'customer_address/region', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_telephone', 'customer_address/telephone', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_fax', 'customer_address/fax', 'default_shipping', null, 'left')
    ->joinAttribute('shipping_vat_id', 'customer_address/vat_id', 'default_shipping', null, 'left');
5
1

Writing an SQL query which does what you want is quite complex with magentos collections.

I recommend to implement your own customer class (and I'm NOT talking about a rewrite here!) which encapsulates the whole process.

Join both addresses into the collection, then you have YOUR customer model which contains both. Now you can return either the shipping address if available, the billing address if available or throw an Exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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