Magento 1.9 – How to Join Customer Collection with Customer Address

customer-addressmagento-1.9modulePHPquery

In Magento i want to search the customer with the address(state,city,zipcode), can you explain to me how to the achieve that.

The customer collection query

$collection = Mage::getModel('customer/customer')->getCollection();
$collection->addAttributeToSelect(array('userid','firstname','lastname','profile_image','specialist','latitude','free_consult','longitude'));
$collection->addAttributeToFilter('group_id',Federallawyer_Customer_Helper_Data::LAWYER);

also my shipping and billing address is same.

Best Answer

$collection = Mage::getModel('customer/customer')->getCollection()
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->addFieldToFilter('billing_postcode','123456') // replace with your post code
            ->addFieldToFilter('billing_city','test') // replace with your city
            ->addFieldToFilter('billing_region','test') // replace with your region
Related Topic