How to Get Customer by Email in Magento 2
Customer data management is something that an admin cannot go away with while administering a Magento 2 store.
Anything from marketing to customer experience, shipping, etc. requires the admin to get customer data for working efficiently.
For instance, you want to implement validation based on customer groups, run an age-group-focused marketing campaign, or offer after-sales service to customers.
All these things require to get customer data in Magento 2.
One way to achieve this is to get all orders of a customer by email ID, which provides a comprehensive view of their purchasing history and enables targeted marketing efforts. This data can be accessed through Magento 2’s customer management system, allowing for personalized and effective customer engagement.
Usually, one can get a customer by ID in Magento 2. However, if you do not have a customer ID, I will show how to get customer by Email in Magento 2!
You can get customer id, first name, last name, and other customer-specific data by just passing the customer email id using the below code.
Method to Get Customer by Email in Magento 2
Use the below code in helper file Data.php at Vendor/Extension/Helper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php namespace Vendore\Extension\Helper; use Magento\Customer\Model\CustomerFactory; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Store\Model\StoreManagerInterface; class Data extends AbstractHelper { protected $customer; protected $storemanager; public function __construct( Context $context, CustomerFactory $customer, StoreManagerInterface $storemanager ) { $this->customer = $customer; $this->storemanager = $storemanager; parent::__construct($context); } public function getCustomerByEmail($email) { $websiteID = $this->storemanager->getStore()->getWebsiteId(); $customer = $this->customer->create()->setWebsiteId($websiteID)->loadByEmail($email); return $customer; } } |
That’s all!
If you are stuck anywhere by using this solution, do mention them in the Comment section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
I am trying to extract customer emails from a specific store on my magento platform. Any help is greatly appriciated.
Hey Jon, Try Doing it with Customer Collection in Magento 2 and filter it by store_id,
Also Refer https://meetanshi.com/blog/get-customer-collection-in-magento-2/