cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 Custom Variables in Email Template

Highlighted

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>
{{/if}}

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:

https://magento.stackexchange.com/questions/160382/magento-2-send-different-email-templates-for-ship...

 

Thanks, Stan

3 REPLIES 3
Highlighted

Re: Magento 2 Custom Variables in Email Template

UPDATE:

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="http://www.w3.org/2001/XMLSchema-instance" 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"/>
</type>
</config>

and then this in my email template:

 

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

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

 

Here it is on Magento StackExchange:

 

https://magento.stackexchange.com/questions/321545/magento-2-email-template-if-shipping-method-then-...

 

Thanks!

Highlighted

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 https://gist.github.com/tiagones/e2f3f822515e2fe0d23d

 

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!

 

 

 

 

Highlighted

Re: Magento 2 Custom Variables in Email Template

Hi,

@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.

 

Thanks!