Get default Billing/Shipping address of logged in customer Magento 2.0

12,212

The correct way to get the billing address of logged in customer is :

$billingID =  $customerSession->getCustomer()->getDefaultBilling();
$address = $objectManager->create('Magento\Customer\Model\Address')->load($billingID);
echo "<pre>";print_r($address->getData());exit;
Share:
12,212
Ian
Author by

Ian

Updated on June 07, 2022

Comments

  • Ian
    Ian almost 2 years

    This is my first project using Magento 2. I'm having a hard time getting the default billing/shipping address of the logged in customer to display at the frontend of my custom module.

    So far I have this:

    //this gets the billing id which is an integer. I'm thinking it must be loaded to get the whole data of the address
    $billingId =  $customerSession->getCustomer()->getDefaultBilling();
    
    //just found this in the internet and thought it might be the same as loading an order, but it doesn't work
    $address = $objectManager->create('\Magento\Customer\Model\AddressFactory')->load($billingId);
    

    But the error says: Call to undefined method Magento\Customer\Model\AddressFactory::load()

    I think I'm close but I don't know what to do next. Thanks in advance.

  • Bartosz Kubicki
    Bartosz Kubicki over 7 years
    Using object manager directly is not good habit. What is more, here should be proper repository for address used.
  • LefterisL
    LefterisL about 7 years
    Instead of pointing out the "wrong" way to do it and then just leave why not write a suggestion of how it's really suppsoed to be implemented.