You can get customer information by just passing the customer email using below code snippet, Create Block file,
To Fetch Customer Data in Magento by customer email ID, you required a customer email id and an optional website id to fetch the correct data.
<?php
declare(strict_types=1);
namespace Rbj\Customer\Model;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
class CustomerDetail
{
public function __construct(CustomerRepositoryInterface $customerRepository )
{
$this->customerRepository = $customerRepository;
}
/**
* @param string $email
* @param ?int $websiteId
* @return CustomerInterface
* @throws LocalizedException
*/
public function getCustomerByEmail(string $email, int $websiteId = null): CustomerInterface
{
try {
$customer = $this->customerRepository->get($email, $websiteId);
} catch (NoSuchEntityException $exception) {
throw new LocalizedException(__('Provided Customer no longer exists.'));
}
return $customer;
}
}
Continue reading “How to get customer data by customer email in magento 2?”