How to Get Customer Data by Customer ID in magento 2?

In an eCommerce store, in order to maintain the inventory, you might require to get order item collection by item ID in Magento 2. The below-mentioned code can help you get order customer data by customer ID in Magento 2.

<?php
namespace MageDelight\Customer\Block;
class Customer extends \Magento\Framework\View\Element\Template
{
 /**
 * Constructor
 *
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param array $data
 */
 public function __construct(
 \Magento\Framework\View\Element\Template\Context $context,
 \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
 array $data = []
 ) {
 $this->customerRepository = $customerRepository;
 parent::__construct($context, $data);
 }


/* Pass customer id $id*/
 public function getCustomer($id)
 { 
 return $this->customerRepository->getById($id);
 }

Now, Call function from Template file

$customerId = 1; //pass dynamic customer id
$getCustomer = $block->getCustomer($customerId);
echo $customer->getFirstname(); // result customer first name
echo $customer->getEmail(); // result as customer email 
echo $customer->getLastname(); // customerr lastname

We hope we covered everything related to getting order customer data by customer ID in Magento 2. If we missed out on anything, feel free to reach us out!

Also read:

How to Get Customer ID using GraphQL in Magento 2?

How to Get Customer Data by Customer email in Magento 2?