How to Get Customer Information By Id in Magento 2?

In this quick article, I will show you how to get the customer information by customer id in Magento 2. In the below example, I will be using both Dependency Injection (DI) and Object Manager to get the customer information (i.e, Firstname, Lastname, Email, Billing Address, Shipping Address, etc).

Using Dependency Injection (DI)

Here is the example code to get the customer information (i.e, Firstname, Lastname, Email, Billing Address, Shipping Address, etc) by customer id in Magento 2 using dependency injection.

In this, we might need to inject the object of \Magento\Customer\Model\CustomerFactory class to get the customer data and \Magento\Customer\Model\AddressFactory class to get the customer address data in the constructor of our module’s block class and access it from the view ( .phtml ) file.

Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php

<?php
namespace YourCompanyName\YourModuleName\Block;

class YourCustomBlock extends \Magento\Framework\View\Element\Template
{    
    protected $_customerFactory;

    protected $_addressFactory;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\AddressFactory $addressFactory,
        array $data = []
    ) {
        $this->_customerFactory = $customerFactory;
        $this->_addressFactory = $addressFactory;
        parent::__construct($context, $data);
    }
    
    public function getCustomerById($id) {
        return $this->_customerFactory->create()->load($id);
    }

    public function getCustomerAddressById($addressId) {
        return $this->_addressFactory->create()->load($addressId);
    }
}

Now, we can use the functions in our view (.phtml) file as follows.

// get customer by id
$customer = $block->getCustomerById(5);

echo $customer->getFirstname() . '<br>';
echo $customer->getLastname() . '<br>';
echo $customer->getEmail() . '<br>';

// get the customer default billing address
$billingAddressId = $customer->getDefaultBilling();
$address = $block->getCustomerAddressById($billingAddressId);
echo '<pre>';
    print_r($address->getData());
echo '</pre>';

// get the customer default shipping address
$shippingAddressId = $customer->getDefaultShipping();
$address = $block->getCustomerAddressById($shippingAddressId);
echo '<pre>';
    print_r($address->getData());
echo '</pre>';

Using Object Manager

Here is the example code to get the customer information (i.e, Firstname, Lastname, Email, Billing Address, Shipping Address, etc) by customer id in Magento 2 using object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 

$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create();

/**
 * Code to get the customer information using customer id
 */
$customer = $customerFactory->load(5); 

echo $customer->getFirstname() . '<br>';
echo $customer->getLastname() . '<br>';
echo $customer->getEmail() . '<br>';

/**
 * Code to get the customer default billing address
 */
$billingAddressId = $customer->getDefaultBilling();
$address = $objectManager->get('Magento\Customer\Model\AddressFactory')->create()->load($billingAddressId);
echo '<pre>';
    print_r($address->getData());
echo '</pre>';

/**
 * Code to get the customer default shipping address
 */
$shippingAddressId = $customer->getDefaultShipping();
$address = $objectManager->get('Magento\Customer\Model\AddressFactory')->create()->load($shippingAddressId);
echo '<pre>';
    print_r($address->getData());
echo '</pre>';

Note: Magento recommends using the dependency injection method to retrieve the customer information. Whether you are developing an extension or a theme, avoid using the object manager method as you may receive technical rejection while submitting your extension to the Magento marketplace.

Read: Programmatically Create New Customer Account in Magento 2

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *