Magento 2 programatically load default billing and shipping address..


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