Filter Customers by a custom field

Filter Customers by a custom field

August 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Filter Customers by a custom field

How to Filter Magento Customers by Custom Fields

Are you struggling with filtering customers in your Magento store based on custom fields like mobile numbers? In this guide, I’ll walk you through the process of selecting customers using their phone numbers.

Understanding Customer Attributes

Magento allows you to add custom attributes to customers. These can be used to store additional information beyond the default name and email fields. In your case, let’s assume you’ve added a phone_number attribute for storing customer mobile numbers.

To check if this attribute exists:

$customerAttribute = Mage::getSingleton('eav/config')
    ->getAttribute('customer', 'phone_number');

if ($customerAttribute->getId()) {
    // The phone_number attribute is available.
}

Loading Customers by Custom Attribute

Magento’s customer model provides the loadCollectionByAttribute() method which can be used to load customers based on a specific attribute value.

Here’s how you can use it:

$phoneNumber = '1234567890';

$customers = Mage::getModel('customer/customer')->loadCollectionByAttribute('phone_number', $phoneNumber);

foreach ($customers as $customer) {
    // Process each customer object.
}

In this example, replace '1234567890' with the actual mobile number you’re searching for. The $customers variable will contain an array of customer objects that match the given phone number.

Processing Customer Objects

Each customer object in the collection has properties like getId(), getName(), and getPhoneNumber() which you can use to access the customer’s data.

For example:

foreach ($customers as $customer) {
    echo 'Customer ID: ' . $customer->getId() . PHP_EOL;
    echo 'Name: ' . $customer->getName() . PHP_EOL;
    echo 'Phone Number: ' . $customer->getPhoneNumber() . PHP_EOL;
}

Best Practices and Further Reading

  1. Use the Search Function: Magento’s default search functionality can also be used to find customers based on custom attributes. This is more user-friendly for your storefront.

  2. Optimize For Speed: If you have a large customer base, consider indexing your phone_number attribute for faster searches.

  3. Check Customer Existence: Before processing the collection, check if it has any elements:

if (count($customers) > 0) {
    // There are customers matching the criteria.
}

For more advanced filtering techniques like multiple attributes or complex queries, I recommend exploring Magento’s EAV (Entity Attribute Value) system. You can find detailed explanations in this comprehensive guide from Laravel Company: Understanding Magento EAV System

Conclusion

Filtering customers by custom fields is a common requirement for many Magento stores, especially when dealing with customer-specific information like phone numbers. By using the built-in loadCollectionByAttribute() method and understanding how to access attribute values in the customer object, you can easily implement this functionality.

Remember, proper indexing of your custom attributes can significantly improve search performance as your customer base grows.

If you have any other questions about Magento development or need help with a specific project, feel free to reach out to us at Laravel Company. We specialize in delivering high-quality solutions tailored to your business needs.

Configuration Email Customers

Our network