2

In magento 2 how to add custom in data sales order confirmation mail template

enter image description here

  • Custom data means? Any dynamic data or static data? – Ranganathan Jun 4 at 8:43
  • dynamic data from config setting – divya sekar Jun 4 at 8:47
  • Amit bera solution will works perfectly...pls use that one – Ranganathan Jun 4 at 10:31
1

First, you have to pass you to order using event email_order_set_template_vars_before.

Fire an observer on this event and this observer pass your parameter and its value.

<?php

namespace StackExchange\Magento\Observer;

class OrderSender implements \Magento\Framework\Event\ObserverInterface {

    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger;

    public function __construct(
     \Psr\Log\LoggerInterface $logger
    ) {

        $this->logger = $logger;
    }
    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        $this->logger->debug(__METHOD__);

        $transportObject = $observer->getEvent()->getTransportObject();
        // new paremter
        $transportObject->setData('custom_field_1','value1');

    }
} 

After that to show, you need to customization that order template from admin.

Check out the link https://devdocs.magento.com/guides/v2.1/frontend-dev-guide/templates/template-email.html

Then add {{var custom_field_1}} to print data at the email template.

  • its working good, but i want to know by the above way is there any chances to get a ordered product id or sku – divya sekar Jun 4 at 13:17
  • i want to do some logic condition in observer to display a custom variable is there any chance to get a ordered product id or sku in above observer – divya sekar Jun 4 at 13:19

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.