Can i get customer address like picture below with address id? If i can, how should i do it?

customer shipping address


You can get the formatted address using address id with 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;
  • can i place that code to block? – Edwin Widhiyanto Sep 26 '17 at 7:53
  • Yes you can add the code in block – Pankaj Pareek Sep 26 '17 at 8:16
  • I tried your code, but nothing showed when i call the function in phtml – Edwin Widhiyanto Sep 26 '17 at 8:29
  • 1
    I have updated the code and tested. please check. @max: I have used this to just make reference to add this code with existing construct code ---- – Pankaj Pareek Sep 26 '17 at 8:56
  • 1
    Rather use // comments instead of code-breaking fragments. :) – Max Sep 26 '17 at 11:43

You can get the order as follows wherever you want to invoke this information add following in the Constructor if not already being used.

protected $checkoutSession; public function __construct( \Magento\Checkout\Model\Session $checkoutSession, \Psr\Log\LoggerInterface $logger ) {

    $this->checkoutSession = $checkoutSession;
    $this->logger = $logger;

$order = $session->getLastRealOrder();
$orderdata = $order->getData();
$shipping_address_id = $orderdata['shipping_address_ID'];

This address is the final shipping address selected during payment stage of the checkout process.

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.