Here we are going to explain, how to get billing and shipping address by customer id in Magento 2?
We need to create a module for that.
Let’s call it – Magemonkeys_Customeraddress
The next step is to create a block file – Block/Address.php
<?php
namespace MagemonkeysCustomeraddressBlock;
class Address extends MagentoFrameworkViewElementTemplate
{
protected $accountManagement;
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCustomerApiAccountManagementInterface $accountManagement,
array $data = []
) {
$this->accountManagement = $accountManagement;
parent::__construct($context, $data);
}
public function getDefaultShippingAddress($customerId)
{
try {
$address = $this->accountManagement->getDefaultShippingAddress($customerId);
} catch (NoSuchEntityException $e) {
return __('You have no shipping address');
}
return $address;
}
public function getDefaultBillingAddress($customerId)
{
try {
$address = $this->accountManagement->getDefaultBillingAddress($customerId);
} catch (NoSuchEntityException $e) {
return __('You have no billing address');
}
return $address;
}
}
Then, we need to call block function in phtml file w we can create view/customeraddress.phtml file
$customerId = 11; echo $this->getDefaultShippingAddress($customerId); echo $this->getDefaultBillingAddress($customerId);
Last, but not least, clear your cache to check the output.

