Showing results for 
Search instead for 
Did you mean: 

Magento 2 Custom Variables in Email Template


Magento 2 Custom Variables in Email Template

I have a custom shipping module and I am trying to execute some information in an email template based on the shipping method chosen. I have tried things like:

{{if order.getShippingDescription() == 'Shipping Method - Shipping Title'}}
<p>{{trans "Works!"}}</p>

How can I target the shipping method and show information based on that method in the email template?


Or, how can I do something like this:


Thanks, Stan


Re: Magento 2 Custom Variables in Email Template


I have tried creating a plugin in Vendor/Module/Plugin/Order/Email/Container/EmailCode.php:


class EmailCode
public function beforeSetTemplateVars(\Magento\Sales\Model\Order\Email\Container\Template $subject, array $vars)
/** @var Order $order */
$order = $vars['order'];
$method = $order->getShippingMethod();

$vars['shipping_code'] = $method === 'freedelivery_freedelivery';

return [$vars];


and I added in etc/frontend di.xml:


<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Sales\Model\Order\Email\Container\Template">
<plugin name="email.shipping.code" type="Vendor\Module\Plugin\Order\Email\Container\Template\EmailCode" sortOrder="100"/>

and then this in my email template:


{{if shipping_code}}
<p>{{trans "Email Code Works"}}</p>

And it didn't work. Any help is appreciated!


Here it is on Magento StackExchange:




Re: Magento 2 Custom Variables in Email Template

You can do this withoud making changes in files. Magento has created tempate varibales for the transactional email templates. you can find all variables form the


order.getShippingDescription() is not woring in you case. you can try other valid template vars.



{{var order.getShippingMethod(1).getMethod()}}
{{var order.getShippingCarrier().getConfigData('title')}}


Find helpful? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!






Re: Magento 2 Custom Variables in Email Template


@Kapil_Thakur  I am not sure if that will do what I need.  If a certain shipping method is used I need to show some information.


Anyhow, I did it by adding an Observer.  If anyone wants the code the solution is in the Stack Exchange link in my original post.