We have added a plugin to add extra custom fields in checkout page of magento2 using this plugin. https://github.com/sbodak/magento2-checkout-custom-form

It save this information in Order, and this infomation can be seen if we open order in backend. enter image description here

You can also check the fields of this plugin in checkout page in front end here. enter image description here

And here is the code of plugin which is used to show this order fields in Backend. enter image description here

Now i want to show this information in Order email which is sent to customer automatically after each order. Here is the screen shot of that email template in Marketing->Email Templates. Here you can see in template code there are variables to show different order information. Can i use some variable here which will show this new custome fields of Delivery Date and Delivery Time etc in email templetes? enter image description here


Add a new module to your mangento installation. in my case Badi\Customvariable add etc/events.xml and add the following code.

<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="email_order_set_template_vars_before">
        <observer name="badi_add_custom_variable_to_Order" instance="Badi\Customvariable\Observer\ObserverforAddCustomVariable" />

Now add Observer/ ObserverforAddCustomVariable.php and add the following code

    namespace Badi\Customvariable\Observer;
    use Magento\Customer\Api\CustomerRepositoryInterface;
    use Magento\Framework\Event\ObserverInterface;
    class ObserverforAddCustomVariable implements ObserverInterface
        protected $customerRepository;
        public function __construct(
            CustomerRepositoryInterface $customerRepository
        ) {
            $this->customerRepository = $customerRepository;
        public function execute(\Magento\Framework\Event\Observer $observer)
            /** @var \Magento\Framework\App\Action\Action $controller */
            $transport = $observer->getEvent()->getTransport();
            if($transport->getOrder() != null)
                $transport['deliverydate'] = $transport->getOrder()->getCheckoutDeliveryDate(); 
                $transport['deliverytime'] = $transport->getOrder()->getCheckoutDeliveryTime();  

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.