customer_information_magento_2 customer_information_magento_2

Get Customer Information From Customer ID in Magento 2

In this code snippet, we will see how to fetch customer information such as email, first name, last name, billing address and shipping address from customer id. You can get logged in customer id from customer session object.
 

Note: For the demonstrated purpose we have used Objectmanager.Codextblog never recommend the direct use of ObjectManager.One should always use a constructor method to instant an object.

Get Customer Information From Customer ID

<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();

$customerId = 1;

$customer = $customerFactory->load($customerId);

//fetch whole customer information
echo "<pre>";
print_r($customer->getData());

//fetch specific information
echo $customer->getEmail();
echo $customer->getFirstName();
echo $customer->getLastName();

Get Customer Default Billing Address


$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();

$customerId = 1;

$customer = $customerFactory->load($customerId);

$billingAddressId = $customer->getDefaultBilling();
$address = $objectManager->get('MagentoCustomerModelAddressFactory')->create()->load($billingAddressId);

echo $address->getData('firstname')."<br>";
echo $address->getData('lastname')."<br>";
echo $address->getData('street')."<br>";
echo $address->getData('city')."<br>";
echo $address->getData('region')."<br>";
echo $address->getData('postcode')."<br>";
echo $address->getData('country_id')."<br>";

Get Customer Default Shipping Address


$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();

$customerId = 1;

$customer = $customerFactory->load($customerId);

$shippingAddressId = $customer->getDefaultShipping();
$address = $objectManager->get('MagentoCustomerModelAddressFactory')->create()->load($shippingAddressId);

echo $address->getData('firstname')."<br>";
echo $address->getData('lastname')."<br>";
echo $address->getData('street')."<br>";
echo $address->getData('city')."<br>";
echo $address->getData('region')."<br>";
echo $address->getData('postcode')."<br>";
echo $address->getData('country_id')."<br>";

Get Customer All Addresses


$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();

$customerId = 1;

$customer = $customerFactory->load($customerId);

$Addresses = $customer->getAddresses();

foreach ($Addresses as $address){

    echo "<pre>";
    print_r($address->getData());
}

If you liked this post, then please like us on Facebook and follow us on Twitter.

Written by

Chirag

Chirag is a Founder & eCommerce Technology Leader at MageArmy. He leads the eCommerce development team and drives the company’s technology vision, helping businesses build, scale, and optimize high-performing online stores. With years of experience in eCommerce development, he has worked with businesses across industries to solve complex technical challenges and create scalable digital commerce solutions. He loves sharing practical insights about eCommerce development, Magento, Adobe Commerce, and the latest trends in digital commerce to help online businesses grow and succeed.