2

I want to allow the customer to Log in using email or mobile number on the checkout page and the login page.

Note: Mobile number is customer eav attribute.

2

3 Answers 3

4

For Login with the customer, First you need to make mobile number required field in customer Registration form. Whenever customer register, At that time you need to add a field like mobile no. and save that field to Customer registration page.

Now when customer login at that time they enter email id or mobile number. For this you need to override this controller

Magento\Customer\Controller\Account\LoginPost

in your module and add code like below, Path should be like: app/code/Vendor/Modulename/Controller/Account/LoginPost.

Now keep below code in your controller,

<?php
namespace Vendor\Modulename\Controller\Account;

use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Customer\Model\Account\Redirect as AccountRedirect;

class LoginPost extends \Magento\Customer\Controller\Account\LoginPost
{
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Api\AccountManagementInterface $customerAccountManagement,
        CustomerUrl $customerHelperData,
        Validator $formKeyValidator,
        AccountRedirect $accountRedirect,
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory
    ) {
        $this->_customerFactory = $customerFactory;
        parent::__construct($context,$customerSession,$customerAccountManagement,$customerHelperData,$formKeyValidator,$accountRedirect);
    }

    public function execute()
    {       
        if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setPath('*/*/');
            return $resultRedirect;
        }
        if ($this->getRequest()->isPost()) {
            $login = $this->getRequest()->getPost('login');

            if(!strpos($login['username'], '@') !== false ) {
                $isMobile = $login['username']; 
                /* Get email id based on mobile number and login*/
                $customereCollection = $this->_customerFactory->create();
                $customereCollection->addFieldToFilter("mobile", $login['username']);
                foreach($customereCollection as $customerdata){ 
                    $login['username'] = $customerdata['email'];
                }
            }


            if (!empty($login['username']) && !empty($login['password'])) {
                try {
                    $customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
                    $this->session->setCustomerDataAsLoggedIn($customer);
                    $this->session->regenerateId();
                    if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
                        $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
                        $metadata->setPath('/');
                        $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
                    }
                    $redirectUrl = $this->accountRedirect->getRedirectCookie();

                    if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {

                        $this->accountRedirect->clearRedirectCookie();
                        $resultRedirect = $this->resultRedirectFactory->create();
                        // URL is checked to be internal in $this->_redirect->success()
                        $resultRedirect->setUrl($this->_redirect->success($redirectUrl));
                        return $resultRedirect;
                    }else{
                        $resultRedirect = $this->resultRedirectFactory->create();
                        $resultRedirect->setPath('');
                        return $resultRedirect;
                    }
                } catch (EmailNotConfirmedException $e) {
                    $value = $this->customerUrl->getEmailConfirmationUrl($login['username']);
                    $message = __(
                        'This account is not confirmed. <a href="%1">Click here</a> to resend confirmation email.',
                        $value
                    );
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);
                } catch (UserLockedException $e) {
                    $message = __(
                        'You did not sign in correctly or your account is temporarily disabled.'
                    );
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);
                } catch (AuthenticationException $e) {
                    $message = __('You did not sign in correctly or your account is temporarily disabled.');
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);
                } catch (LocalizedException $e) {
                    $message = $e->getMessage();
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);
                } catch (\Exception $e) {
                    // PA DSS violation: throwing or logging an exception here can disclose customer password
                    $this->messageManager->addError(
                        __('An unspecified error occurred. Please contact us for assistance.')
                    );
                }
            } else {
                $this->messageManager->addError(__('A login and a password are required.'));
            }
        }

        return $this->accountRedirect->getRedirect();
    }
}

I hope this will help

2
  • on checkout page, how can i change input type email to text ? so user can add their mobile number. Apr 12, 2019 at 5:57
  • 1
    @CharviParikh override template Magento_Checkout/template/form/element/email.html Apr 12, 2019 at 6:25
0

Charvi Parikh@: can you share complete module i need same for as per my need

0

As the asked functionality is unavailable in default Magento 2, either you have to custom code or you may use Magento 2 Mobile Login extension to allow customers login using email or mobile number on the checkout page and the login page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.