15/08/2022
We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
To get the customer address data by address id in Magento 2
Directory
Create the GetCustomerAddress.php file in the directory mentioned below,
app/code/VendorName/ModuleName/Model/GetCustomerAddress.php
Content of GetCustomerAddress.php
<?php
namespace VendorName\ModuleName\Model;
use Magento\Customer\Api\AddressRepositoryInterface;
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;
}
}