In Magento 2 there are many methods available to load customer data by using ID and email.
This blog helps you to understand how to load customers by ID as well as how to load customers by Email in Magento 2.
First, we will see how we can load customer by using id:
How to Load Customer by ID in Magento 2
To load customer by ID, there are three methods available:
Let’s check out these methods.
Get Customer by ID Using API Repository Method
In this method to load Customer by using ID.
Here we have used Magento\Customer\Api\CustomerRepositoryInterface instance and created its 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:
<?php $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 |
Load Customer by ID Using Factory Method
In this method to load customer by using ID.
Here we have used Magento\Customer\Model\Customer instance and created its 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
}
}
|
Our certified Magento 2 experts build, customize, and optimize your store for peak performance.
Load Customer by ID Using Object Manager Method
<?php
$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
|
How to Load Customer by Email in Magento 2
Now, we will see how we can load customer by using email:
Let’s see these methods.
Get Customer by Email Using API Repository Method
In this method to load customer by using email.
Here we have used Magento\Customer\Api\CustomerRepositoryInterface instance and created it’s object ($customerRepository) and used Magento\Store\Model\StoreManagerInterface instance 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:
<?php $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 |
Get on-demand access to certified developers who solve issues fast and keep your store running smoothly.
Load Customer by Email Using Factory Method
In this method to load Customer by using ID.
Here we have used Magento\Customer\Model\Customer instance and created its object ($customer) and used Magento\Store\Model\StoreManagerInterface instance and created its 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
}
}
|
Load Customer by Email Using Object Manager Method
<?php
$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 detailed blog.
Conclusion
We hope the 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.