2

I am using below code to get customer id by email

use Magento\Customer\Model\Session;

protected $_objectManager;
protected $currentCustomer;
protected $_customer; 
protected $_storeManager;    
 public function __construct(
    Session $customerSession,
    \Magento\Customer\Model\CustomerFactory $customer,
    \Magento\Framework\ObjectManagerInterface $objectmanager,
    \Magento\Store\Model\StoreManagerInterface $storeManager
)
{        
    $this->session = $customerSession;        
    $this->_customer = $customer;       
    $this->_objectManager = $objectmanager;
    $this->_storeManager = $storeManager; 
}

 public function execute()
{
  $email = '[email protected]';
 $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.

1
  • Can you please upload your full code ? Or use my below code. Commented Sep 26, 2018 at 15:07

2 Answers 2

8

Use this below code :

protected $_customer;
protected $_storemanager;
public function __construct(
    .....
    \Magento\Customer\Model\CustomerFactory $customer,
    \Magento\Store\Model\StoreManagerInterface $storemanager,
    .....
){
    .....
    $this->_customer = $customer;
    $this->_storemanager = $storemanager;
    ...
}
public function execute() {
    $email = '[email protected]';
    $websiteID = $this->_storemanager->getStore()->getWebsiteId();
    $customer = $this->_customer->create()->setWebsiteId($websiteID)->loadByEmail($email);
    $customerId = $customer->getId();
}
8
  • I am also using same code, but not working for me, tell me what is the difference in my code Commented Sep 26, 2018 at 15:07
  • please check my updated answer Commented Sep 26, 2018 at 15:11
  • same again customer id is not returning Commented Sep 26, 2018 at 15:15
  • please check my updated answer. Remove var and generated folder Commented Sep 26, 2018 at 15:18
  • not working i tried that also, Uncaught Error: Call to undefined method Magento\\Customer\\Model\\ResourceModel\\CustomerRepository\\Interceptor::getByEmail() Commented Sep 26, 2018 at 15:22
3

There is another way you can get Customer id by Customer Email.In this use service contact Class \Magento\Customer\Api\CustomerRepositoryInterface

namespace {NameSpace};


class {YOUR_CLass}
{

    /**
     * @var \Magento\Customer\Api\CustomerRepositoryInterface
     */
    protected $customerRepository;

    public function __construct(
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
    )
    {
        $this->customerRepository = $customerRepository;
    }

    /**
     * @return \Magento\Customer\Api\Data\CustomerInterface
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function  getCustomer()
    {

        try{
            $customer = $this->customerRepository->get('[email protected]');
            $customerId = $customer->getId();
        }catch(\Magento\Framework\Exception\NoSuchEntityException $exception){

        }catch(\Magento\Framework\Exception\LocalizedException $exception){

        }
        return $customer;
    }
}

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.