Sunday, July 9, 2017

Login With Username/ Mobile in Magento 1.x






Magento provides login functionality for login to account with email and password but sometimes you want to login with customer username/mobileno with account password than here below describe how you implement this functionality in Magento 1. x modify some changes.

Here first add new customer attribute as mobile/username according to your requirement by create sql file in any extension or run installer command as below.

$installer = $this;
$installer->startSetup();
$setup = Mage::getModel('customer/entity_setup', 'core_setup');
$setup->addAttribute('customer', 'mobile', array(
    'type' => 'varchar',
    'input' => 'text',
    'label' => 'Mobile No',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'default' => '0',
    'visible_on_front' => 1,
));
$installer->endSetup();

After execute below code in your mysql file or directly from any Magento page.Mobile No field display on website admin panel see add or edit any customer there is display mobile No field. Now you must set login with mobile no in magento front for that changes in `loginPostAction` function located in 'app/code/core/Mage/Customer/controllers/AccountController.php' as below.

public function loginPostAction()
{
    $loginpost = $this->getRequest()->getPost();
    $mobileno = $loginpost['login']['username'];
    if (filter_var($mobileno, FILTER_VALIDATE_EMAIL) //check username as email or mobile.
    {
     parent::loginPostAction(); //excute current code
    }
    else
    {
     $session = Mage::getSingleton('customer/session');
        $collection = Mage::getModel('customer/customer')->getCollection();
        $website_id = Mage::app()->getWebsite()->getId();
        $collection->addAttributeToFilter('mobile', array('eq' =>$mobileno));
        $custData = $collection->getData();
        $email = trim($custData[0]['email']);
        $customerId = (int) trim($custData[0]['entity_id']);
        try{
         $authenticateuser = Mage::getModel('customer/customer')->setWebsiteId($website_id)->authenticate($email, $username['login']['password']);
       }catch( Exception $e ){
            $session->addError('Invalid Login Detail');
            $this->_redirect('customer/account');
       }      
       try{
         if($authenticateuser && $customerId){
             $customer = Mage::getModel('customer/customer')->load($customerId);
                $session->setCustomerAsLoggedIn($customer);
                $message = $this->__('You are now logged in as %s', $customer->getName());
                $session->addSuccess($message);
            }
            else{
             throw new Exception ($this->__('The login attempt was unsuccessful. Some parameter is missing Or wrong data '));
            }
      }
      catch (Exception $e){$session->addError($e->getMessage());}
      $this->_redirect('customer/account');
    }
} 
 
Before make changes in below file you must remove email validation from your theme login. phtml file because default Magento provides only login with email so you must remove login textbox validation by removing validate-email` class of the textbox.
Here customer login using mobileno/username. You can set customer attribute as unique for preventing duplication. You can also check below logic before saving any customer as below.

$customer = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('mobileno')->addAttributeToFilter('mobileno',{Val})->load();
if (is_object($customer)){
 //Customer already exits with same mobileno
}

No comments :

Post a Comment