Magento 2 – Programmatically Load Default Billing and Shipping Address

billing-addresscustomer-addressmagento2shipping-address

Magento 2 programatically load default billing and shipping address..

Best Answer

This is an example of getting default customer's billing and shipping address if you have the customerId and using the customerRepository Magento\Customer\Api\CustomerRepositoryInterface and the addressRepository Magento\Customer\Api\AddressRepositoryInterface

 $customer = $this->customerRepository->getById($customerId);
 $billingAddressId = $customer->getDefaultBilling();
 $shippingAddressId = $customer->getDefaultShipping();

//get default billing address
 try {
    $billingAddress = $this->addressRepository->getById($billingAddressId);
} catch (\Exception $e) {
    //
}
Related Topic