Magento 2 get Customer Details


Magento 2 get Customer Details– It is totally different to get the customer details in Magento 2. You can use object manager or service contracts(\Magento\Customer\Api\CustomerRepositoryInterface). Here in this tutorial we are going to explain how to get customer details using customer id or customer session. We will cover the other details like -email, name, shipping address, billing address details etc.


Magento 2 get Customer Details Example

Magento 2 get Customer Details

First of all, we need to get the customer object either by session or load customer by id. Let us go one by one-

Load Customer Data By Id

You can use object manager to load the customer by id simply as below –

Magento 2 get Customer Details Example:

$customerId = 101;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);

Get Customer Data From Session

If customer is logged in you can get the customer object from session using object manager simply as below-

Magento 2 Get Customer Session Example:| Get current customer Id

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $customerSession = $objectManager->get('Magento\Customer\Model\Session');
    if($customerSession->isLoggedIn()) {
     $customer = $customerSession->getCustomer();
     $id = $customer->getId(); // customer Id
     }

Using the above method you can check whether customer is logged in or not. Now let us get more details about the customer using the above customer object.

  • Customer Id
  • Customer Name(FirstName, LastName)
  • Customer Email Id
  • Customer Group Id

Get Customer Id | First Name | Last Name | Email Id | Customer Group Id

Get Customer Data in Magento 2 Example:

$customerId = 101;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);
$firstname = $customer->getFirstname();
$lastname = $customer->getLastname();
$email = $customer->getEmail();
$groupId = $customer->getGroupId();

The above example will give you the customer details.


Get Customer Address Details


  • Default Shipping Address Details
  • Default Billing Address Details

Get Shipping Address Details | By Default Address Id

Customer’s Default Shipping Address Can Be Loaded Using the Default Address Id. You can get the following shipping details from Shipping Address Object-

  • Shipping FirstName
  • Shipping LastName
  • Shipping Company
  • Shipping Zip Code
  • Shipping City
  • Shipping Street
  • Shipping Telephone
  • Shipping Country

Load Customer Address By Address Id Example:

$shippingAddressID =  $customerSession->getCustomer()->getDefaultBilling();
$shippingAddress = $objectManager->create('Magento\Customer\Model\Address')->load($shippingAddressID);
$shippingFirstname = $shippingAddress->getFirstname();
$shippingLastname = $shippingAddress->getLastname();
$shippingCompany = $shippingAddress->getCompany();
$shippingZipcode= $shippingAddress->getPostCode();
$shippingCompany = $shippingAddress->getCompany();
$shippingStreet = $shippingAddress->getStreet();
$shippingStreet1 = $shippingStreet[0];
$shippingStreet2 = $shippingStreet[1];
$shippingTelephone = $shippingAddress->getTelephone();
$shippingFax = $shippingAddress->getFax();
$shippingCountry = $shippingAddress->getCountry();

Get Billing Address Details | By Default Address Id

Customer’s Default Billing Address Can Be Loaded Using the Default Address Id. You can get the following shipping details from Billing Address Object-

  • Billing FirstName
  • Billing LastName
  • Billing Company
  • Billing Zip Code
  • Billing City
  • Billing Street
  • Billing Telephone
  • Billing Country

Load Customer Billing Address By Billing Id Example:

$shippingAddressID =  $customerSession->getCustomer()->getDefaultBilling();
$billingAddress = $objectManager->create('Magento\Customer\Model\Address')->load($shippingAddressID);
$billingFirstname = $billingAddress->getFirstname();
$billingLastname = $billingAddress->getLastname();
$billingCompany = $billingAddress->getCompany();
$billingZipcode= $billingAddress->getPostCode();
$billingCompany = $billingAddress->getCompany();
$billingStreet = $billingAddress->getStreet();
$billingStreet1 = $billingStreet[0];
$billingStreet2 = $billingStreet[1];
$billingTelephone = $billingAddress->getTelephone();
$billingFax = $billingAddress->getFax();
$billingCountry = $billingAddress->getCountry();

Advertisements

Add Comment

📖 Read More