How to load customer by id and by email in Magento 2
In Magento 2 there are many method are available to load Customer Data by Using Id and Email.
This Blog help you to understand how we load customer by Id and Email in Magento 2.
First, We will see how we can load customer by using id :
Load Customer by id in Magento2 :
To load Customer by Id,there are three methods available : 1) Using Api Repository Method, 2)Using Factory Method and 3)Using Object Manager Method. Let’s See these Methods :
Using Api Repository Method:
In this method to load Customer by using Id ,Here we have used Magento\Customer\Api\CustomerRepositoryInterface instance and create it’s object( $customerRepository).
<?php namespace Mageants\Blog\Block; use Magento\Framework\View\Element\Template\Context; use Magento\Customer\Api\CustomerRepositoryInterface; class Index extends \Magento\Framework\App\Action\Action {  protected $customer;  public function __construct( Context $context, CustomerRepositoryInterface $customerRepository, array $data = []  ) { $this->customerRepository = $customerRepository; parent::__construct($context, $data);  }  public function getCustomerData($id)  { return $this->customerRepository->getById($id);  } } |
You have to call above function in phtml file to get customer information as below :
 $customerId = 1; //pass dynamic customer id  $getCustomer = $block->getCustomerData($customerId); // by using this variable you can get all information of customer as below  echo $getCustomer->getFirstname(); // result customer first name  echo $getCustomer->getEmail(); // result as customer email  echo $getCustomer->getLastname(); // customerr lastname |
Using Factory Method
In this method to load Customer by using Id ,Here we have used Magento\Customer\Model\Customer instance and create it’s object( $customer).
<?php namespace Mageants\Blog\Block; use Magento\Framework\View\Element\Template\Context; use Magento\Customer\Model\Customer; class Index extends \Magento\Framework\App\Action\Action {  protected $customer;  public function __construct( Context $context, Customer $Customer, array $data = []  ) { $this->Customer = $Customer; parent::__construct($context, $data);  }  public function getCustomerData()  { $customerId = 1; //pass dynamic customer id $customerData = $this->Customer->load($customerId); return $customerData->getFirstname(); // result customer first name  } } |
Using Object Manager Method
 $customerId = 1; //pass dynamic customer id  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();  $customerData = $objectManager->create('Magento\Customer\Model\Customer')->load($customer_id);   echo $customerData->getFirstname(); // result customer first name |
Load Customer by Email in Magento2:
Now , We will see how we can load customer by using email :
To load Customer by Id,there are three methods available : 1) Using Api Repository Method, 2)Using Factory Method and 3)Using Object Manager Method. Let’s See these Methods :
Using Api Repository Method:
In this method to load Customer by using Email ,Here we have used Magento\Customer\Api\CustomerRepositoryInterface instance and create it’s object( $customerRepository) and used Magento\Store\Model\StoreManagerInterfaceinstance and created it’s object (storeManager).
<?php namespace Mageants\Blog\Block; use Magento\Framework\View\Element\Template\Context; use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Store\Model\StoreManagerInterface; class Index extends \Magento\Framework\App\Action\Action {  protected $customer;  public function __construct( Context $context, CustomerRepositoryInterface $customerRepository, StoreManagerInterface $storeManager, array $data = []  ) { $this->customerRepository = $customerRepository; $this->storeManager = $storeManager; parent::__construct($context, $data);  }  public function getCustomerData($customerEmail)  { $websiteId = $this->storeManager->getStore()->getWebsiteId(); // by this here we get the website id return $this->customerRepository->get($customerEmail,$websiteId);  } } |
You have to call above function in phtml file to get customer information as below :
 $customer_email='roni_cost@example.com'; //pass dynamic customer id  $getCustomer = $block->getCustomerData($customer_email); // by using this variable you can get all information of customer as below  echo $getCustomer->getId(); // result as customer Id  echo $getCustomer->getFirstname(); // result as customer firstname  echo $getCustomer->getLastname(); // result as customer lastname |
Using Factory Method
In this method to load Customer by using Id ,Here we have used Magento\Customer\Model\Customer instance and create it’s object( $customer) and used Magento\Store\Model\StoreManagerInterfaceinstance and created it’s object (storeManager).
<?php namespace Mageants\Blog\Block; use Magento\Framework\View\Element\Template\Context; use Magento\Customer\Model\Customer; use Magento\Store\Model\StoreManagerInterface; class Index extends \Magento\Framework\App\Action\Action {  protected $customer;  public function __construct( Context $context, Customer $Customer, StoreManagerInterface $storeManager, array $data = []  ) { $this->Customer = $Customer; $this->storeManager = $storeManager; parent::__construct($context, $data);  }  public function getCustomerData()  { $customer_email = 'roni_cost@example.com'; //pass dynamic customer email $websiteId = $this->storeManager->getStore()->getWebsiteId(); // by this here we get the website id $customerData = $this->Customer->setWebsiteId($websiteId)->loadByEmail($customerId); return $customerData->getFirstname(); // result customer first name  } } |
Using Object Manager Method
 $customer_email = 'roni_cost@example.com'; //pass dynamic customer Email  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();  $customerData = $objectManager->create('Magento\Customer\Model\Customer');  customerData->setWebsiteId(1);  customerData->loadByEmail($customer_email);   echo $customerData->getFirstname(); // result customer first name |
If you want to understand about How to Load Order By Id in Magento 2 Programmatically then, you can read our blog by click Here.
Conclusion:
We hope above blog helps you to clearly understand How To Load Customer By Using Id and email in Magento 2 Programmatically.
In case of any kind of problem with the above code implementation, you can contact us or let us know in comment section.