The post gives the solution to get customer address data by address ID in Magento 2.
The Magento 2 store owners may sometimes employ techniques such as restricting the shipping of fragile or heavy items in certain areas to optimize the profit.
Also, managing the shipping process can be easy with organizing the customer addresses, including the ability to add and manage multiple shipping addresses. By allowing customers to save multiple shipping addresses, it becomes easier for them to choose the correct address during the checkout process.
However, to implement such a system needs to get customer address data. The below code is an easy programmatic way to get customer address information using address ID in Magento 2 store.
Customer address data is got by Address ID using AddressRepositoryInterface interface.
Method to Get Customer Address Data by Address ID in Magento 2:
Create GetCustomerAddress.php Class in app/code/Vendor/Extension/Model:
<?php namespace VendorExtensionModel; use MagentoCustomerApiAddressRepositoryInterface; class GetCustomerAddress { private $addressRepository; public function __construct( AddressRepositoryInterface $addressRepository ) { $this->addressRepository = $addressRepository; } public function getAddressDataById($addressId) { try { $addressData = $this->addressRepository->getById($addressId); } catch (Exception $exception) { throw new Exception($exception->getMessage()); }; return $addressData; } }
Now you can access getAddressData($addressId) function as per your requirement by extending GetCustomerAddress class
That’s all for customer address data.
Please share the solution with the Magento community via social media.
Thank you.