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
<?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!
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.