get customer id by email magento2?

by jafar pinjar   Last Updated September 26, 2018 15:09 PM

I am using below code to get customer id by email

  $email = 'test@gmail.com';
 $customerModel = $this->_objectManager->create('Magento\Customer\Model\Customer');
        $storeId = $this->getWebsiteId();
        $customerModel->setWebsiteId($storeId);
        $customerModel->loadByEmail($email);
        $userId = $customerModel->getId();

 public function getWebsiteId(){
   return $this->_storeManager->getStore()->getWebsiteId();
}

I am not getting customerid, Please anyone suggest me, how to get customer id by email.



Answers 1


Use this below code :

$customerEmail = 'test@gmail.com';
$CustomerModel = $objectManager->create('Magento\Customer\Model\Customer');
$CustomerModel->setWebsiteId(1); **//Here 1 means Store ID**
$CustomerModel->loadByEmail($customerEmail);
$userId = $CustomerModel->getId();

Note : Do not use object manager directly.

Rohan Hapani
Rohan Hapani
September 26, 2018 15:06 PM

Related Questions



How to add customer Id in table magento 2

Updated November 18, 2017 12:09 PM


Only allow 6 digit number as customer password

Updated April 02, 2018 05:09 AM

Fatal error in Constructor controller in Magneto2

Updated August 07, 2018 06:09 AM