How to Use Custom Variables in Transactional Email in Magento 2

Email marketing can be tricky business but the pay-offs are remarkable. Crafting a high-quality Email is difficult and when it comes to transactional Emails, they are the “must-see” emails. It contains information about the order that the customers are interested in and it must contain all the required details.

According to a study by Experian, transactional emails receive 8 times the opens compared to non-transactional marketing emails.

For Magento 2 store owners, this is an issue of concern as the default Magento 2 order Email is not up to the mark! The store owner may want to add customer-specific details, additional order details, etc. for delivering more information or for a personalized approach. This leads to using the custom variable in Magento 2 Emails.

To overcome this default limitation, here’s the solution to use custom variables in transactional Email in Magento 2 for optimizing the order Emails!

Method to use custom variables in transactional Email in Magento 2:

1. Create registration.php file in app\code\[Vendor]\[Namespace]\

<?php
        \Magento\Framework\Component\ComponentRegistrar::register(
            \Magento\Framework\Component\ComponentRegistrar::MODULE,
            '[Vendor]_[Namespace]',
            __DIR__
        );

2. Create module.xml file in app\code\[Vendor]\[Namespace]\etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Meetanshi_Gdpr" setup_version="1.0.2"/>
</config>

3. Create events.xml file in app\code\[Vendor]\[Namespace]\etc

<?xml version="1.0"?>
<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="meetanshi_add_custom_variable_to_Order"
                  instance="[Vendor]\[Namespace]\Observer\ObserverforAddCustomVariable"/>
    </event>
</config>

4. Create ObserverforAddCustomVariable.php file in app\code\[Vendor]\[Namespace]\Observer

<?php
namespace [Vendor]\[namespace]\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) 
        {
            $customer = $this->customerRepository->getById($transport->getOrder()->getCustomerId());
            if ($customer->getCustomAttribute('username')) 
            {
                $transport['username'] = $customer->getCustomAttribute('username')->getValue();
            }
        }
    }
}

You can use custom Email variable in email template like “{var username}”

With the above method, you can optimize your order Emails!

Thank you.

Table of Contents