Get Customer by Email in Magento 2

0
6806
Get Customer by Email in Magento 2
Get Customer by Email in Magento 2

It is easily to get customer model instance by id, but what if your input is customer email? I will show you how to get customer by email in Magento 2 in this post.

To query customer data, first we need to inject an instance of \Magento\Customer\Api\CustomerRepositoryInterface into constructor.

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

public function getCustomer($email)
{
    return $this->customerRepository->get($email);
}

After what, you only need to call getCustomer($email) to get customer model instance.

If there are multiple websites in your Magento store, you can pass 2nd parameters to specify which website to query customer data from.

$this->customerRepository->get($email, $websiteId);

Have fun ~