{"id":440,"date":"2019-05-29T13:30:26","date_gmt":"2019-05-29T13:30:26","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/05\/29\/use-custom-variables-in-transactional-email-in-magento-2\/"},"modified":"2025-05-22T16:48:37","modified_gmt":"2025-05-22T11:18:37","slug":"use-custom-variables-in-transactional-email-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/use-custom-variables-in-transactional-email-in-magento-2\/","title":{"rendered":"How to Use Custom Variables in Transactional Email in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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 \u201cmust-see\u201d emails. It contains information about the order that the customers are interested in and it must contain all the required details.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">According to a study by Experian, transactional emails receive 8 times the opens compared to non-transactional marketing emails.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To overcome this default limitation, here\u2019s the solution to use custom variables in transactional Email in Magento 2 for optimizing the order Emails!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to use custom variables in transactional Email in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>registration.php<\/strong>&nbsp;file in&nbsp;<em><em><strong><em><em><strong>app\\code\\[Vendor]\\[Namespace]<\/strong><\/em><\/em><strong>\\<\/strong><\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n        \\Magento\\Framework\\Component\\ComponentRegistrar::register(\n            \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n            '[Vendor]_[Namespace]',\n            __DIR__\n        );<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>module.xml<\/strong>&nbsp;file in&nbsp;<em><em><strong><em><em><strong>app\\code\\[Vendor]\\[Namespace]\\etc<\/strong><\/em><\/em><\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Meetanshi_Gdpr\" setup_version=\"1.0.2\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>events.xml<\/strong>&nbsp;file in&nbsp;<em><em><strong><em><em><strong>app\\code\\[Vendor]\\[Namespace]\\etc<\/strong><\/em><\/em><\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event\/etc\/events.xsd\">\n    &lt;event name=\"email_order_set_template_vars_before\">\n        &lt;observer name=\"meetanshi_add_custom_variable_to_Order\"\n                  instance=\"[Vendor]\\[Namespace]\\Observer\\ObserverforAddCustomVariable\"\/>\n    &lt;\/event>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create&nbsp;<strong>ObserverforAddCustomVariable.php<\/strong>&nbsp;file in&nbsp;<em><em><strong><em><em><strong>app\\code\\[Vendor]\\[Namespace]\\Observer<\/strong><\/em><\/em><\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace [Vendor]\\[namespace]\\Observer;\nuse Magento\\Customer\\Api\\CustomerRepositoryInterface;\nuse Magento\\Framework\\Event\\ObserverInterface;\nclass ObserverforAddCustomVariable implements ObserverInterface\n{\n    protected $customerRepository;\n    public function __construct(CustomerRepositoryInterface $customerRepository)\n    {\n        $this->customerRepository = $customerRepository;\n    }\n    public function execute(\\Magento\\Framework\\Event\\Observer $observer)\n    {\n        \/** @var \\Magento\\Framework\\App\\Action\\Action $controller *\/\n        $transport = $observer->getEvent()->getTransport();\n        if ($transport->getOrder() != null) \n        {\n            $customer = $this->customerRepository->getById($transport->getOrder()->getCustomerId());\n            if ($customer->getCustomAttribute('username')) \n            {\n                $transport['username'] = $customer->getCustomAttribute('username')->getValue();\n            }\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can use custom Email variable in email template like \u201c{var username}\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With the above method, you can optimize your order Emails!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-440","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=440"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/440\/revisions"}],"predecessor-version":[{"id":15404,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/440\/revisions\/15404"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}