How can we get address id based get address details! I got all address details getting based on customer id but i need to get address details based on address id?How can i achieve this
1 Answer
You can get the formatted address using the address id with the below code:
public function __construct(
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
\Magento\Customer\Model\Address\Config $addressConfig,
\Magento\Customer\Model\Address\Mapper $addressMapper,
) {
$this->addressRepository = $addressRepository;
$this->_addressConfig = $addressConfig;
$this->addressMapper = $addressMapper;
}
public function getFormattedAddress($addressId)
{
try {
$addressObject = $this->addressRepository->getById($addressId);
/** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
$renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
return $renderer->renderArray($this->addressMapper->toFlatArray($addressObject));
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
return null;
}
}