Load customer by email and customer Id in Magento 2

In Magento 2, Developer needs to load customer by email or sometime load customer by customer Id. Today we are going to load customer by email and customer id with different way.

Load customer by email or customer Id using CustomerRepositoryInterface

I am not writing a full code of block or controller. I just add the require resources for load customer by email or customer Id. You need to add that require resources on your block, controller or helper where you need.

<?php

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;

/* Your namespace and class declaration */

/**
 * @var CustomerRepositoryInterface
 */
protected $customerRepository;

/**
 * @var StoreManagerInterface
 */
protected $storeManager;

/**
 * Add this perameter to your class constructor
 *
 * @param CustomerRepositoryInterface $customerRepository
 * @param StoreManagerInterface $storeManager
 */
public function __construct(
    CustomerRepositoryInterface $customerRepository,
    StoreManagerInterface $storeManager
) {
    $this->customerRepository = $customerRepository;
    $this->storeManager = $storeManager;
}

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

/* function for get customer by email */
public function getCustomerByEmail($email)
{
    $websiteId = $this->getStore()->getWebsiteId();
    return $this->customerRepository->get($email, $websiteId);
}

/* function for get store */
protected function getStore()
{
    return $this->storeManager->getStore();
}

Load customer using Object Manager

As per the Magento standard we not need to use ObjectManager anyware in our custom module. We can use object manager for some short of time in phtml file when we developing anything. Once it’s done we need to move that logic in Block or ViewModel.

Also we cna use ObjectManager in our custom script.

<?php
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\State;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;

require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get(State::class);
$state->setAreaCode('frontend');

$customerRepository = $objectManager->create(CustomerRepositoryInterface::class);
$storeManager = $objectManager->create(StoreManagerInterface::class);

$id = 1;
$email = 'rsadadiya@gmail.com';
$websiteId = $storeManager->getStore()->getWebsiteId();

/* Retrive customer by customer Id */
$customer = $customerRepository->getById($id);

/* Retrive customer by email */
$customer1 = $customerRepository->get($email, $websiteId);

Load customer using Factory class

I am not recommend to use this factory class to load any data in Magento 2. Factory class always create the new object while creating the instance of the class. When we need every time new object at that time we can use the factory class.

<?php

use Magento\Customer\Model\CustomerFactory;
use Magento\Store\Model\StoreManagerInterface;

/* Your namespace and class declaration */

/**
 * @var CustomerFactory
 */
protected $customerFactory;

/**
 * @var StoreManagerInterface
 */
protected $storeManager;

/**
 * Add this perameter to your class constructor
 *
 * @param CustomerFactory $customerFactory
 * @param StoreManagerInterface $storeManager
 */
public function __construct(
    CustomerFactory $customerFactory,
    StoreManagerInterface $storeManager
) {
    $this->customerFactory = $customerFactory;
    $this->storeManager = $storeManager;
}

/* function for get customer by id*/
public function getCustomerById($customerId)
{
    return $this->customerFactory->create()->load($customerId);
}

/* function for get customer by email */
public function getCustomerByEmail($email)
{
    $websiteId = $this->getStore()->getWebsiteId();
    return $this->customerFactory->create()->setWebsiteId($websiteId)->loadByEmail($email);
}

/* function for get store */
protected function getStore()
{
    return $this->storeManager->getStore();
}

Load customer in custom script by using Factory class

As per the Magento standard we not need to use ObjectManager anyware in our custom module. We can use object manager for some short of time in phtml file when we developing anything. Once it’s done we need to move that logic in Block or ViewModel.

Also we cna use ObjectManager in our custom script.

<?php
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\State;
use Magento\Customer\Model\CustomerFactory;
use Magento\Store\Model\StoreManagerInterface;

require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get(State::class);
$state->setAreaCode('frontend');

$customerFactory = $objectManager->create(CustomerFactory::class);
$storeManager = $objectManager->create(StoreManagerInterface::class);

$id = 1;
$email = 'rsadadiya@gmail.com';
$websiteId = $storeManager->getStore()->getWebsiteId();

/* Retrive customer by customer Id */
$customer = $customerFactory->create()->load($id);

/* Retrive customer by email */
$customer1 = $customerFactory->create()->setWebsiteId($websiteId)->loadByEmail($email);

These are the way to load customer using email and customer id.

Fill free to ask me any of the Magento issues or contact me if your need any customisation or other Magento related service.

Will provide you these Magento services click here for more details

Leave your comment

Your email address will not be published. Required fields are marked *