Load customer by email in Magento
Another example of how to load customer in Magento - this time - by customer email. Previously we looked at how to load customer by ID in Magento, now we will do the same, but using the customer email address. Here’s how to do it!
Get customer by email in Magento
<?php
// The email of customer you want to load
$customerEmail = 'email@example.com';
// Website ID the customer is assigned to
// Here we are using current website's ID
$websiteId = Mage::app()->getWebsite()->getId();
// Instance of customer loaded by the given email
$customer = Mage::getModel('customer/customer')
->setWebsiteId($websiteId)
->loadByEmail($customerEmail);
Note that customers in Magento can be either assigned to specific website, or be global - meaning that they can log in using same credentials in all websites. It is a default Magento feature that can be configured via admin panel settings (System -> Configuration -> Customers -> Customer Configuration -> Account Sharing Options -> Share Customer Accounts).
Depending on your store config, you will either have to specify the website ID customer is assigned to, or you may as
well skip it. If your customers are registered on per-website basis - you have to specify the website ID - otherwise an
exception will be thrown from the loadByEmail
method.
If, however, your customers are registered on global scope, following example will work as well:
<?php
// The email of customer you want to load
$customerEmail = 'email@example.com';
// Instance of customer loaded by the given email
$customer = Mage::getModel('customer/customer')
->loadByEmail($customerEmail);
// Customer existence check and some data retrieval
if ($customer->getId()) {
var_dump($customer->getId()); // Get customer ID
var_dump($customer->getData()); // Get all customer data
var_dump($customer->getEmail()); // Get customer email address
var_dump($customer->getFirstname()); // Get customer firstname
}
If you are trying to figure out how to load customer by its ID or by custom customer attribute, check these posts: load customer by ID in Magento and load customer by custom attribute in Magento.