I want to login the customer with mobile number instead of email. Can anyone help me?
My AccountController.php file is as follows:
<?php
require_once "Mage/Customer/controllers/AccountController.php";
class User_Tracker_AccountController extends Mage_Customer_AccountController{
public function loginPostAction()
{
if (!$this->_validateFormKey()) {
$this->_redirect('*/*/');
return;
}
if ($this->_getSession()->isLoggedIn()) {
$this->_redirect('*/*/');
return;
}
$session = $this->_getSession();
if ($this->getRequest()->isPost()) {
$login = $this->getRequest()->getPost('login');
//added code
$phoneNumber = $login['username'];
$customer = Mage::getResourceModel('customer/customer')
->addAttributeToSelect('phone_number')
->addAttributeToFilter('phone_number', $phoneNumber)
->getFirstItem()->getCustomer();
if ($customer !== false) {
$login['username'] = $customer->getEmail();
}else{
$login['username'] = $phoneNumber;
}
//end here
if (!empty($login['username']) && !empty($login['password'])) {
try {
$session->login($login['username'], $login['password']);
if ($session->getCustomer()->getIsJustConfirmed()) {
$this->_welcomeCustomer($session->getCustomer(), true);
}
} catch (Mage_Core_Exception $e) {
switch ($e->getCode()) {
case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
$value = $this->_getHelper('customer')->getEmailConfirmationUrl($login['username']);
$message = $this->_getHelper('customer')->__('This account is not confirmed. <a href="%s">Click here</a> to resend confirmation email.', $value);
break;
case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
$message = $e->getMessage();
break;
default:
$message = $e->getMessage();
}
$session->addError($message);
$session->setUsername($login['username']);
} catch (Exception $e) {
// Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
}
} else {
$session->addError($this->__('Login and password are required.'));
}
}
$this->_loginPostRedirect();
}
}
phone_number
attribute? Or are you usingtelephone
number field from address?