7

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.

2 Answers 2

5

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() ?>
3
  • Can you tell me how to get the whole address? Not like getFirstName() or else.
    – Nitesh
    Mar 22, 2017 at 11:56
  • How can I call formattedBillingAddress() function their as like email templates
    – Nitesh
    Mar 22, 2017 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, 2017 at 12:40
3

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));
4
  • 10
    Using object manager directly is not recommended.
    – diazwatson
    Oct 27, 2017 at 0:18
  • getFormattedShippingAddress is protected method you cannot access outside. Jun 14, 2018 at 12:49
  • 2
    You can directly create object for Renderer class. $formatedAddress = $objectManager->get('\Magento\Sales\Model\Order\Address\Renderer'); echo $formatedAddress->format($_order->getShippingAddress(), 'html') Jun 14, 2018 at 12:52
  • i am getting with html tag how to remove it Jun 22, 2018 at 14:33

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.