4

How to login with mobile number in magento-1.9.1.1 without extension?

I created

The file at app/etc/modules/New_Mage.xml

<?xml version="1.0"?>
<config>
 <modules>
      <New_Mage>
           <active>true</active>
           <codePool>local</codePool>
      </New_Mage>
  </modules>
</config>

In app/code/local/New/Mage/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <New_Mage>
            <version>0.0.1</version>
        </New_Mage>
    </modules>
    <frontend>
        <routers>
            <customer>
                <args>
                    <modules>
                        <New_Mage before="Mage_Customer">New_Mage</New_Mage>
                    </modules>
                </args>
            </customer>
        </routers>
    </frontend>
</config>

I already have 'Telephone" field in my admin panel. So, i used the name i.e., telephone in AccountController.php enter image description here

controller will app/code/local/New/Mage/controllers/Customer/AccountController.php

    <?php
    $baseDir= Mage::getBaseDir();
require_once '"'.$baseDir.'"app/code/core/Mage/Customer/controllers/Acco‌‌​​‌​untController.php‌​'‌​; 


class New_Mage_Customer_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');
            if(!Zend_Validate::is($login['username'] , 'EmailAddress')){
                $customer = Mage::getResourceModel('customer/address_collection')
                ->addAttributeToSelect('email')
                ->addAttributeToFilter('telephone', $login['username'] )
                ->getFirstItem();
                if ($customer !== false) {
                    $login['username'] = $customer->getEmail();
                }
            }
            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();
    }


}

My login.phtml file is as below at app/design/frontend/base/default/template/ced/sociallogin/customer/form/login.phtml:

<div class="account-login">
    <div class="page-title">
        <h1><?php echo $this->__('Login or Create an Account') ?></h1>
    </div>
    <?php echo $this->getMessagesBlock()->toHtml() ?>
    <form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="login-form">
        <?php echo $this->getBlockHtml('formkey'); ?>
        <div class="col2-set">
            <div class="col-1 new-users">
                <div class="content">
                    <h2><?php echo $this->__('New Customers') ?></h2>
                    <p><?php echo $this->__('By creating an account with our store, you will be able to move through the checkout process faster, store multiple shipping addresses, view and track your orders in your account and more.') ?></p>
                </div>
            </div>
            <div class="col-2 registered-users">
                <div class="content">
                    <h2><?php echo $this->__('Registered Customers') ?></h2>
                    <p><?php echo $this->__('If you have an account with us, please log in.') ?></p>
                    <ul class="form-list">
                        <li>
                            <label for="email" class="required"><em>*</em><?php echo $this->__('Email Id / Mobile') ?></label>
                            <div class="input-box">
                                <input type="text" name="login[username]" value="<?php echo $this->escapeHtml($this->getUsername()) ?>" id="email" class="input-text required-entry" title="<?php echo $this->__('Email Address') ?>" />
                            </div>
                        </li>
                        <li>
                            <label for="pass" class="required"><em>*</em><?php echo $this->__('Password') ?></label>
                            <div class="input-box">
                                <input type="password" name="login[password]" class="input-text required-entry validate-password" id="pass" title="<?php echo $this->__('Password') ?>" />
                            </div>
                        </li>
                        <?php echo $this->getChildHtml('form.additional.info'); ?>
                        <?php echo $this->getChildHtml('persistent.remember.me'); ?>
                    </ul>
                    <?php echo $this->getChildHtml('persistent.remember.me.tooltip'); ?>
                    <p class="required"><?php echo $this->__('* Required Fields') ?></p>
                    <div class="ced_sociallogin">
                        <?php echo $this->getChildHtml("ced_sociallogin_login"); ?>
                    </div>
                </div>
            </div>
        </div>
        <div class="col2-set">
            <div class="col-1 new-users">
                <div class="buttons-set">
                    <button type="button" title="<?php echo $this->__('Create an Account') ?>" class="button" onclick="window.location='<?php echo Mage::helper('persistent')->getCreateAccountUrl($this->getCreateAccountUrl()) ?>';"><span><span><?php echo $this->__('Create an Account') ?></span></span></button>
                </div>
            </div>
            <div class="col-2 registered-users">
                <div class="buttons-set">
                    <a href="<?php echo $this->getForgotPasswordUrl() ?>" class="f-left"><?php echo $this->__('Forgot Your Password?') ?></a>
                    <button type="submit" class="button" title="<?php echo $this->__('Login') ?>" name="send" id="send2"><span><span><?php echo $this->__('Login') ?></span></span></button>
                </div>
            </div>
        </div>
        <?php if (Mage::helper('checkout')->isContextCheckout()): ?>
            <input name="context" type="hidden" value="checkout" />
        <?php endif; ?>
    </form>
    <script type="text/javascript">
    //<![CDATA[
        var dataForm = new VarienForm('login-form', true);
    //]]>
    </script>
</div>

My Register.phtml file is at below:

app/design/frontend/base/default/template/improvedaddress/customer/form/register.phtml :

My file is as below :

<div class="account-create">
    <div class="page-title">
        <h1><?php echo $this->__('Create an Account to Login') ?></h1>
    </div>
    <?php echo $this->getChildHtml('form_fields_before')?>
    <?php echo $this->getMessagesBlock()->toHtml() ?>
    <?php /* Extensions placeholder */ ?>
    <?php echo $this->getChildHtml('customer.form.register.extra')?>
    <form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="form-validate">
        <div class="fieldset">
            <input type="hidden" name="success_url" value="<?php echo $this->getSuccessUrl() ?>" />
            <input type="hidden" name="error_url" value="<?php echo $this->getErrorUrl() ?>" />


            <h2 class="legend"><?php echo $this->__('Personal Information') ?></h2>

            <div class="ced_sociallogin"><?php echo $this->getChildHtml("ced_sociallogin_login"); ?></div>



            <ul class="form-list">
                <li class="fields">
                    <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getFormData())->setForceUseCustomerAttributes(true)->toHtml() ?>
                </li>
                <li>
                    <label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                    <div class="input-box">
                        <input type="text" name="email" id="email_address" value="<?php echo $this->escapeHtml($this->getFormData()->getEmail()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Email Address')) ?>" class="input-text validate-email required-entry" />
                    </div>
                </li>
                <?php if ($this->isNewsletterEnabled()): ?>
                <li class="control">
                    <div class="input-box">
                        <input type="checkbox" name="is_subscribed" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Sign Up for Newsletter')) ?>" value="1" id="is_subscribed"<?php if($this->getFormData()->getIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox" />
                    </div>
                    <label for="is_subscribed"><?php echo $this->__('Sign Up for Newsletter') ?></label>
                    <?php /* Extensions placeholder */ ?>
                    <?php echo $this->getChildHtml('customer.form.register.newsletter')?>
                </li>
                <?php endif ?>
            <?php $_dob = $this->getLayout()->createBlock('customer/widget_dob') ?>
            <?php if ($_dob->isEnabled()): ?>
                <li><?php echo $_dob->setDate($this->getFormData()->getDob())->toHtml() ?></li>
            <?php endif ?>
            <?php $_taxvat = $this->getLayout()->createBlock('customer/widget_taxvat') ?>
            <?php if ($_taxvat->isEnabled()): ?>
                <li><?php echo $_taxvat->setTaxvat($this->getFormData()->getTaxvat())->toHtml() ?></li>
            <?php endif ?>
            <?php $_gender = $this->getLayout()->createBlock('customer/widget_gender') ?>
            <?php if ($_gender->isEnabled()): ?>
                <li><?php echo $_gender->setGender($this->getFormData()->getGender())->toHtml() ?></li>
            <?php endif ?>
            </ul>
        </div>
    <?php if($this->getShowAddressFields()): ?>
        <div class="fieldset">
            <input type="hidden" name="create_address" value="1" />
            <h2 class="legend"><?php echo $this->__('Address Information') ?></h2>
            <ul class="form-list">
                <li class="fields">
                    <div class="field">
                        <label for="company"><?php echo $this->__('Company') ?></label>
                        <div class="input-box">
                            <input type="text" name="company" id="company" value="<?php echo $this->escapeHtml($this->getFormData()->getCompany()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Company')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('company') ?>" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="telephone" class="required"><em>*</em><?php echo $this->__('Telephone') ?></label>
                        <div class="input-box">
                            <input type="text" name="telephone" id="telephone" value="<?php echo $this->escapeHtml($this->getFormData()->getTelephone()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Telephone')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>" />
                        </div>
                    </div>
                </li>
            <?php $_streetValidationClass = $this->helper('customer/address')->getAttributeValidationClass('street'); ?>
                <li class="wide">
                    <label for="street_1" class="required"><em>*</em><?php echo $this->__('Street Address') ?></label>
                    <div class="input-box">
                        <input type="text" name="street[]" value="<?php echo $this->escapeHtml($this->getFormData()->getStreet(1)) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Street Address')) ?>" id="street_1" class="input-text <?php echo $_streetValidationClass ?>" />
                    </div>
                </li>
            <?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?>
            <?php for ($_i = 2, $_n = $this->helper('customer/address')->getStreetLines(); $_i <= $_n; $_i++): ?>
                <li class="wide">
                    <div class="input-box">
                        <input type="text" name="street[]" value="<?php echo $this->escapeHtml($this->getFormData()->getStreet($_i)) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Street Address %s', $_i)) ?>" id="street_<?php echo $_i ?>" class="input-text <?php echo $_streetValidationClass ?>" />
                    </div>
                </li>
            <?php endfor; ?>
                <li class="fields">
                                        <div class="field">
                        <label for="region_id" class="required"><em>*</em><?php echo $this->__('State/Province') ?></label>
                        <div class="input-box">
                            <select id="region_id" name="region_id" title="<?php echo Mage::helper('core')->quoteEscape($this->__('State/Province')) ?>" class="validate-select" style="display:none;">
                                <option value=""><?php echo $this->__('Please select region, state or province') ?></option>
                            </select>
                            <script type="text/javascript">
                            //<![CDATA[
                                $('region_id').setAttribute('defaultValue', "<?php echo $this->getFormData()->getRegionId() ?>");
                            //]]>
                            </script>
                            <input type="text" id="region" name="region" value="<?php echo $this->escapeHtml($this->getRegion()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('State/Province')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;" />
                        </div>
                    </div>
                    <div class="field">
                                                <label for="city" class="required"><em>*</em><?php echo $this->__('City') ?></label>
                                                <div class="input-box">
                                                    <select id="city_id" name="city_id" title="<?php echo $this->__('City') ?>" class="validate-select" style="display:none;">
                                                        <option value=""><?php echo $this->__('Please select City') ?></option>
                                                    </select>
                                                    <script type="text/javascript">
                                                    //<![CDATA[
                                                        $('city_id').setAttribute('defaultValue',  "<?php echo $this->getAddress()->getCityId() ?>");
                                                    //]]>
                                                    </script>
                                                    <input type="text" name="city" value="<?php echo $this->escapeHtml($this->getAddress()->getCity()) ?>"  title="<?php echo Mage::helper('core')->quoteEscape($this->__('City')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('city') ?>" id="city" />
                                                </div>
                                        </div>                   
                </li>
                <li class="fields">
                    <div class="field">
                        <label for="zip" class="required"><em>*</em><?php echo $this->__('Zip/Postal Code') ?></label>
                        <div class="input-box">
                            <input type="text" name="postcode" value="<?php echo $this->escapeHtml($this->getFormData()->getPostcode()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Zip/Postal Code')) ?>" id="zip" class="input-text validate-zip-international <?php echo $this->helper('customer/address')->getAttributeValidationClass('postcode') ?>" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="country" class="required"><em>*</em><?php echo $this->__('Country') ?></label>
                        <div class="input-box">
                            <?php echo $this->getCountryHtmlSelect() ?>
                        </div>
                    </div>
                </li>
            </ul>
            <input type="hidden" name="default_billing" value="1" />
            <input type="hidden" name="default_shipping" value="1" />
        </div>
    <?php endif; ?>
        <div class="fieldset">
            <h2 class="legend"><?php echo $this->__('Login Information') ?></h2>
            <ul class="form-list">
                <li class="fields">
                    <div class="field">
                        <label for="password" class="required"><em>*</em><?php echo $this->__('Password') ?></label>
                        <div class="input-box">
                            <input type="password" name="password" id="password" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Password')) ?>" class="input-text required-entry validate-password" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="confirmation" class="required"><em>*</em><?php echo $this->__('Confirm Password') ?></label>
                        <div class="input-box">
                            <input type="password" name="confirmation" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Confirm Password')) ?>" id="confirmation" class="input-text required-entry validate-cpassword" />
                        </div>
                    </div>
                </li>
                <?php echo $this->getChildHtml('form.additional.info'); ?>
            </ul>
        </div>
        <div class="buttons-set">
            <p class="required"><?php echo $this->__('* Required Fields') ?></p>
            <p class="back-link"><a href="<?php echo $this->escapeUrl($this->getBackUrl()) ?>" class="back-link"><small>&laquo; </small><?php echo $this->__('Back') ?></a></p>
            <button type="submit" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Submit')) ?>" class="button"><span><span><?php echo $this->__('Submit') ?></span></span></button>
        </div>
    </form>
    <script type="text/javascript">
    //<![CDATA[
        var dataForm = new VarienForm('form-validate', true);
        <?php if($this->getShowAddressFields()): ?>
        new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>, undefined, 'zip');
                new CityUpdater('country', 'region_id', 'city', 'city_id', <?php echo Mage::helper('improvedaddress')->getCityJson() ?>);               
        <?php endif; ?>
    //]]>
    </script>
</div>

When I tried to login with my registered mobile number, I got Invalid login or password error.

enter image description here

5
  • may in your controller file dont get class in require_once so please cross check and try this solution Commented Mar 2, 2017 at 5:52
  • @MurtuzaZabuawala same error. check once my controller.php. i just modified. Commented Mar 2, 2017 at 6:04
  • require_once '"'.$baseDir.'"app/code/core/Mage/Customer/controllers/Acco‌​‌​untController.php'‌​; check updated code Commented Mar 2, 2017 at 6:07
  • You have not declare $basedir in your code Commented Mar 2, 2017 at 6:50
  • Hi We have add this extension on Magento1.9 its working fine
    – Vishal
    Commented Mar 2, 2017 at 11:04

3 Answers 3

4

There is the extension to log in with mobile or any other username.

  1. This extension provides the solution as any user can register with user name and can login with user name.
  2. This extension provides the options to create user name with characters and with digits.
  3. We can use the limitation to the characters and to the digits.
  4. Find this extension at https://github.com/diglin/Diglin_Username.
1
  • How to login with mobile number in magento-1.9.1.1 without extension?
    – inrsaurabh
    Commented Mar 18, 2018 at 12:36
3

You can do it this way, I hope you know how to override controller in your Custom module so I am not going into that part.
Override Mage_Customer_AccountController to your local module and update loginPostAction function with below code. Put your customer attribute you used to store mobile number in place of telephone Create a controller file AccountController.php in app/code/local/New/Mage/controllers/Customer

<?php
    require_once "Mage/Customer/controllers/AccountController.php";  
    class New_Mage_Customer_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');
            if (!empty($login['username']) && !empty($login['password'])) { 
                try {
                    if(!Zend_Validate::is($login['username'] , 'EmailAddress')){
                        $customerAddress = Mage::getResourceModel('customer/address_collection')
                        ->addAttributeToFilter('telephone', $login['username'] )
                        ->getFirstItem();
                        if($customerAddress !== false){
                            $login['username'] = Mage::getModel('customer/customer')->load($customerAddress->getData('parent_id'))->getEmail();
                        }
                    }
                    $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();
    }
}

You need to validate unique mobile number on customer registration.

9
  • You need to override ` Mage_Customer_AccountController` in a custom module. Commented Feb 11, 2017 at 6:03
  • Refer magento.stackexchange.com/a/92173/2443 to override controller Commented Feb 11, 2017 at 6:31
  • Can you add your full content of app/code/local/New/Mage/controllers/AccountController.php controller file? Commented Feb 23, 2017 at 10:52
  • @TejabhagavanKollepara, I think you need to put your controller in Customer folder, see updated answer. Commented Feb 23, 2017 at 11:10
  • How did you add your telephone field? Is it default field in address section or you have created new field with name telephone? Commented Mar 2, 2017 at 10:41
0

You controller class name is wrong. as you path suggested controller will app/code/local/New/Mage/controllers/Customer/AccountController.php

class New_Mage_Customer_AccountController extends Mage_Customer_AccountController{

test it and then feedback me

here are few change you can do

remove get ->getCustomer()

$customer = Mage::getResourceModel('customer/address_collection')
                    ->addAttributeToSelect('email')
                    ->addAttributeToFilter('customer_mobile', $login['username'] )
                    ->getFirstItem();
9
  • sir i updated with your class , no error now. but, there is only login with email on my website. i didnt edit login.phtml file and register.phtml file. need to edit? Commented Feb 23, 2017 at 11:59
  • where you add the mobile for login yes you have to edit in login and want the mobile number then add it to registration too Commented Feb 23, 2017 at 12:08
  • @TejabhagavanKollepara update the code and kindly update and check Commented Feb 27, 2017 at 6:59
  • yeah, i modified sir. still same as before. plz check controller.php , login and register pages also sir. Commented Feb 27, 2017 at 7:21
  • you attribute is showing in register form? or show i admin? check these first if you had entry then code will be execute Commented Feb 27, 2017 at 7:27

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.