In this post I am going to explain how to get customer data by customer email Magento 2.
Using below code snippet you can get customer data by passing customer email and website id.
Add below code snippet in Block class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | protected $_customerRepository; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, array $data = [] ) { $this->_customerRepository = $customerRepository; parent::__construct($context, $data); } // Get customer data public function getCustomerDataByEmail($customerEmail,$websiteId = null) { return $this->_customerRepository->get($customerEmail,$websiteId); } |
Add below code snippet in template file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | // Customer Email //Website Id $websiteId = 1; // Call getCustomerData function $customerData = $block->getCustomerDataByEmail($customerEmail, $websiteId); //customer first name echo $customerData->getFirstname().'<br/>'; //customer last name echo $customerData->getLastname().'<br/>'; //customer email echo $customerData->getEmail(); // Customer All Data in Array echo "<pre>"; print_r($customerData->__toArray()); echo "</pre>"; |
That’s it. Enjoy Magento 2!!
Write an article about ecommerce that help people to grow their ecommerce business. You’ll find best ecommerce guide, news, tips & more!
Leave a Reply