How to load customer by email in magento2?

18 Aug, 2021

Today I am going to show you how we can get customer by email in Magento 2

If you need to get customer in Controller, Block, Helper or Observer then please follow the code


    <?php 
    /* Your namespace and class declaration */

    protected $customerRepository;

    /* add Magento\Customer\Api\CustomerRepositoryInterface to you constructor */
    public function __construct( 
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
    ) {
        $this->customerRepository = $customerRepository;
    }

    /* function for get customer by email id*/
    public function getCustomer($email)
    {
        return $this->customerRepository->get($email);
    }

If you wan't to get customer in .phtml file then follow the code


<?php

$customerEmail = "abc@example.com";
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerRepository = $objectManager->get("Magento\Customer\Api\CustomerRepositoryInterface");
$customer = $customerRepository->get($customerEmail); // get customer data using email id

?>

RAJU SADADIYA (MAGENTO DEVELOPER)