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
s@gmail.com
shipping infog@gmail.com
shipping infok@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');
Best Answer
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
.