It's possible to implement the basic logic with little custom code:
Write an observer for controller_predispatch_customer_account_loginPost
, check if the posted email address (username
) does not look like an email address. In this case look up the customer by customer number and replace the username field in the POST data with the actual email address of the found customer.
Example observer code:
$request = $observer->getControllerAction()->getRequest();
$username = $request->getPost('username');
if (false === strpos($username, '@')) {
$customer = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToFilter('customer_number', $username)
->getFirstItem();
if ($customer && $customer->getEmail()) {
$request->setPost('username', $customer->getEmail());
}
}
Then in controller_postdispatch_customer_account_loginPost
, replace the email address in the session back to the customer number, if it is set. Otherwise error messages on unsuccessful logins will contain the email address instead of the number.
Example observer code:
$emailAddress = Mage::getSingleton('customer/session')->getUsername();
if ($emailAddress) {
$customerNumber = Mage::getModel('customer/customer')->loadByEmail($emailAddress)
->getCustomerNumber();
}
Mage::getSingleton('customer/session')->setUsername($customerNumber);
Other things to consider:
- Apply same logic to "forgot password" form
- Decide if you want to use the built in
increment_id
as customer number, which you can enable in the system configuration under Customer Configuration > Create New Account Options > Generate Human-Friendly Customer ID