I need to retrieve the customer object belonging to a certain (mobile) phone number. How can I do this in Magento?

And how can I get a customer by phone number when a phone number was entered by end users and can be written in different ways?

For instance:

  • +1 310-954-8012
  • 1 (310) 954-8012
  • 1-310-954-8012
  • 13109548012
  • Aren't you forgetting that phone numbers aren't necessarily unique? There might be multiple customers with the same phone number. Sure, phone companies keep a close eye on it but for example one person might have signed up twice or someone makes a mistake when entering. I'm assuming your use case is something like customer support, to automatically link incoming calls to an existing customer object. Wouldn't it be better to record the customer's name so you can more accurately get the correct record?
    – Jacques
    Commented Mar 8, 2017 at 10:22
  • "Should I do it?" is a different question than "How to do it?", so separately from the use case. In my case it was a website using mobile phone numbers, so you can assume that the number belongs to a single person. Also we checked if the phone number didn't already exist, just like the email address should be unique in the Magento core application. Including this all would make the question too broad...
    – 7ochem
    Commented Mar 8, 2017 at 11:01

1 Answer 1


You can do this via the customer address collection as the phone number is part of a customer's address.

If the phone number given by $phoneNumber and the one in the customer's address literally match, you can simply do:

$customer = Mage::getResourceModel('customer/address_collection')
    ->addAttributeToFilter('telephone', $phoneNumber)
if ($customer !== false) {
    // Do stuff...

If you need to filter out miscellaneous characters, you need to do this on both sides, input and SQL, so you could do:

$charFilter = array('+', ' ', '(', ')', '-');
$phoneNumber = str_replace($charFilter, '', $phoneNumber);

// Build up SQL string replacements    
$sqlFilter = 'at_telephone.value';
foreach ($charFilter as $_char) {
    $sqlFilter = sprintf('REPLACE(%s, "%s", "")', $sqlFilter, $_char);

// Build collection
$collection = Mage::getResourceModel('customer/address_collection')
    ->addAttributeToFilter('telephone', array('notnull' => true));
$collection->getSelect()->where($sqlFilter . ' = ?', $phoneNumber);

// Get customer
$customer = $collection->getFirstItem()->getCustomer();
if ($customer !== false) {
    // Do stuff...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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