I am working on custom email template.

So I needed customer Billing and Shipping address in below file.

/vendor/magento/module-sales/view/frontend/templates/email/items.phtml

Can any 1 help me to get customer address of placed order.

You can add this piece of code in the file:

<?php /** @var $_order \Magento\Sales\Model\Order */ ?>
<?php $_order = $block->getOrder() ?>
<?php $billingAddress = $_order->getBillingAddress(); ?>
<?php $shippingAddress = $_order->getShippingAddress(); ?>

With $billingAddress now you can get address e.g:

<?php echo $billingAddress->getStreet() ?>
  • Can you tell me how to get the whole address? Not like getFirstName() or else. – Nitesh Mar 22 '17 at 11:56
  • How can I call formattedBillingAddress() function their as like email templates – Nitesh Mar 22 '17 at 12:09
  • Solved my problem by following : $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $formatedAddress = $objectManager->get('\Magento\Sales\Model\Order\Email\Sender\OrderSender');print_r($formatedAddress->getFormattedBillingAddress($_order)); print_r($formatedAddress->getFormattedShippingAddress($_order)); – Nitesh Mar 22 '17 at 12:40
up vote 3 down vote accepted

Solved my problem by following edit in below file:

/vendor/magento/module-sales/view/frontend/templates/email/items.phtml

$_order = $block->getOrder()
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$formatedAddress = $objectManager->get('\Magento\Sales\Model\Order\Email\Sender‌​\OrderSender');
print‌​_r($formatedAddress-‌​>getFormattedBilling‌​Address($_order)); 
print_r($formatedAddress->getFormattedShippingAddress($_orde‌​r));
  • 5
    Using object manager directly is not recommended. – diazwatson Oct 27 '17 at 0:18
  • getFormattedShippingAddress is protected method you cannot access outside. – Mahendra Jella Jun 14 at 12:49
  • 1
    You can directly create object for Renderer class. $formatedAddress = $objectManager->get('\Magento\Sales\Model\Order\Address\Renderer'); echo $formatedAddress->format($_order->getShippingAddress(), 'html') – Mahendra Jella Jun 14 at 12:52
  • i am getting with html tag how to remove it – Er Sarvesh V Tiwari Jun 22 at 14:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.