1

I created a custom attribute customer "cm_telephone", then i rewrite Customer/controllers/AccountController.php loginPostAction() function to customise it for login with the email or phone number. It works, just I wanted to know what you think about my code.

The code:

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');

            if (!empty($login['username']) && !empty($login['password'])) {

                //the phone number condition 
                if (strpos($login['username'], '@') === false){
                    Mage::log('Customer has entered a phone number ');
                    $customer = Mage::getModel('customer/customer')
                        ->getCollection()
                        ->addAttributeToFilter('cm_telephone', $login['username'])
                        ->getFirstItem();
                        if($login['username'] == $customer->getCmTelephone()){
                          $login['username'] = $customer->getEmail();
                        }
                }
                //end phone number condition
                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();
    }

I found this but I didn't want to use an event and I keep the email address and I add the phone.

1 Answer 1

3

Your code is just fine. You can also remove the if condition:

if($login['username'] == $customer->getCmTelephone()){
      $login['username'] = $customer->getEmail(); 
}

And can use just

$login['username'] = $customer->getEmail();

Because you already have got the customer with phone number filter by your code

addAttributeToFilter('cm_telephone', $login['username'])
1

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.