Login customer with phone number without using third party extension [duplicate]
-
07-10-2020 - |
Domanda
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();
}
}
Soluzione
To override controller, use below code in your config.xml
<config>
...... // YOUR OTHER CODE
......
<frontend>
<routers>
<customer>
<args>
<modules>
<User_Tracker before="Mage_Customer">User_Tracker</User_Tracker>
</modules>
</args>
</customer>
</routers>
</frontend>
......
......
</config>
Your controller file is correct and has correct class names.
Just update your code
$phoneNumber = $login['username'];
$customer = Mage::getResourceModel('customer/customer_collection')
->addAttributeToSelect('phone_number')
->addAttributeToFilter('phone_number', $phoneNumber)
->getFirstItem();
if ($customer !== false) {
$login['username'] = $customer->getEmail();
}else{
$login['username'] = $phoneNumber;
}
And it should work.
Altri suggerimenti
If you have an phone number associate to a customer
if (strpos($login['username'], '@') === false){ //the user has entred a phone number
$telUser = $login['username'];
$customer = Mage::getModel('customer/customer')->getCollection()
->addAttributeToFilter('phone_number', $telUser)
->getFirstItem();
if($telUser == $customer->getPhoneNumber()){
$login['username'] = $customer->getEmail();
}
}
ou can try with another extension,
if you want to login with mobile or any other user name you can use this https://github.com/diglin/Diglin_Username extension. but becareful with extensions.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange