In Magento, Customer model has an option addAttributeToFilter which helps to filter customers by attributes. For example, If We would like to get customers collections with group_id(Customer group) 3, we can use as following
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('group_id', array('eq' => 123123))
->load();
for user defined attribute is also same. Here customer_number is user-defined attribute:
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('customer_number', array('eq' => 123123))
->load();
Multiple attributes also can used with “AND” Condition
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('customer_number', array('eq' => 123123))
->addAttributeToFilter('group_id', array('eq' => 3))
->load();
Following work for OR condtion for two attributes:
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(
array(
array('customer_number', array('eq' => 123123))
array('group_id', array('eq' => 3))
)
->load();
For individual afterwards:
foreach ($customers as $customer)
{
var_dump($customer->getData());
}
If the attribute is unique value then We can directly get Row:
$customer = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('customer_number', array('eq' => 123123))
->addAttributeToFilter('group_id', array('eq' => 3))
->load()
->getFirstItem();
Finally, If we need only selected attributes like firstname, lastname and email:
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('firstname')
->addAttributeToSelect('lastname')
->addAttributeToSelect('email');
->addAttributeToFilter('group_id', array('eq' => 3))
->load();