magento2,email-templates – How to Set Values of Billing and Shipping Info in Custom Email Templates

email-templatesmagento2

This is my email_template.html

 <table class="order-details">
                <tr>
                    <td class="address-details">
                        <h3>{{trans "Billing Info"}}</h3>
                        <p>{{var  formattedBillingAddress|raw}}</p>
                    </td>
                     {{depend order.getIsNotVirtual()}}
                    <td class="address-details">
                        <h3>{{trans "Shipping Info"}}</h3>
                        <p>{{var formattedShippingAddress|raw}}</p>
                    </td>
                    {{/depend}}
                </tr>
</table>

and in my observer

$templateVars = array(
            'store' => $this->storeManager->getStore(),
            'message'   => 'We processed your order ID We will contact you soon in mail for the acknowledgement if you not receive mail within 4 hours please get help from support@xxx.com',
            'order' => $order,
            'store'=> $store,
            'productName'=> $productName
       );

->setTemplateVars($templateVars)

My question is how can i set values of billing info, shipping info and payment method in this email template.

Best Answer

Inject class Renderer used for formatting an order address and some classes for Payment into your constructor.

/**
 * @var \Magento\Sales\Model\Order\Address\Renderer
 */
protected $addressRenderer;

 /**
 * @var \Magento\Payment\Helper\PaymentHelper
 */
protected $paymentHelper;

public function __construct(
    \Magento\Sales\Model\Order\Address\Renderer $addressRenderer,
    \Magento\Payment\Helper\PaymentHelper $paymentHelper
) {
    $this->addressRenderer = $addressRenderer;
    $this->paymentHelper = $paymentHelper;
}

Add to $templateVars:

$templateVars = array(
        'store' => $this->storeManager->getStore(),
        'message'   => 'We processed your order ID We will contact you soon in mail for the acknowledgement if you not receive mail within 4 hours please get help from support@xxx.com',
        'order' => $order,
        'store'=> $store,
        'productName'=> $productName,
        'payment_html' => $this->getPaymentHtml($order),
        'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
        'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
   );

Build the methodes for shippping , billing address and payment:

/**
 * @param Order $order
 * @return string|null
 */
protected function getFormattedShippingAddress($order)
{
    return $order->getIsVirtual()
        ? null
        : $this->addressRenderer->format($order->getShippingAddress(), 'html');
}

/**
 * @param Order $order
 * @return string|null
 */
protected function getFormattedBillingAddress($order)
{
    return $this->addressRenderer->format($order->getBillingAddress(), 'html');
}

/**
 * Get payment info block as html
 *
 * @param Order $order
 * @return string
 */
protected function getPaymentHtml(Order $order)
{
    return $this->paymentHelper->getInfoBlockHtml(
        $order->getPayment(),
        $this->identityContainer->getStore()->getStoreId()
    );
}

See a good sample here:

vendor/magento/module-sales/Model/Order/Email/Sender/OrderSender.php vendor/magento/module-sales/view/frontend/email/order_new.html