4

Magento 2 programatically load default billing and shipping address..

1 Answer 1

13

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) {
    //
}
4
  • the above code returns only the address id .can you help me to load the address by address id..?
    – srg
    Aug 4, 2017 at 4:52
  • Answer is updated!
    – Nikolas
    Aug 4, 2017 at 7:48
  • this should be added to block or controller?
    – Ramesh KR
    Jan 22, 2018 at 9:14
  • _getAddressHtml($billingAddress ) will foramt the address? Oct 5, 2018 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.