Ian Ulita Ian Ulita - 3 months ago 44
PHP Question

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

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.

Answer

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;
Comments