Magento 2E-CommerceHow To GuideApril 21, 2024Simon Walker

Magento 2: Add Custom Variable to Email Template

Magento 2: Add Custom Variable to Email Template

According to Statista, the global eCommerce revenue is expected to reach US$4.45tn in 2024. While this shows the popularity of eCommerce, it also highlights the growing competition. It wouldn’t be wrong to say that everyone is trying their hands at eCommerce nowadays.

The customers relish this competition since they have much more choice than before. A customer in the US can order products from anywhere in Europe or Asia. From the business perspective, high levels of competition are a headache. Not only must the compete with local competitors but also those based thousands of miles away.

There are several strategies to manage this competition. As a leading Magento development company, we have considerable experience in helping customers devise eCommerce strategies. We believe that personalisation is the key to long-term success. It helps build lasting relationships and improves sales.

Read More About Email Personalisation:

Magento 2 offers a wide range of features for managing customer relationships and email communication is one such feature. Of course, sending generic emails will not help build loyalty. This is where the Magento 2 add variable to email template feature comes into play.

Magento allows users to add custom variables to templates to make the email engaging. The concept of custom variables is not new. They are pieces of information used to perform different operations, hold values, and personalise communications. They are called variables because their values change according to context or programming logic

Although Magento 2 provides plenty of predefined variables, the list is however limited. To meet your business specific needs, you can create custom variables and add them in pages, blocks, and email templates. Let’s see how to create custom variables and how to add them in an email template.

Before we list the steps, we would like to about the Email Templates Extension by FME Extensions.

About the Extension

This extension allows you to hyper personalise your email communications, thereby ensuring greater customer loyalty and engagement. You can choose from a wide variety of mobile-responsive themes as shown below.

email-templates-extension

Email customisation is incomplete without heading your logos, product details, and other relevant information. With this extension, you can add all this and more.

email-customisation

You can also add your social media links to boost traffic to social media channels.

social-media-links

If you are interested in this extension and wish to know more, book a live demo.

Magento 2 Add Custom Variable to Email Template

Step 1: Create a Custom Variable

  • Login to Magento 2 Admin and navigate to System -> Other Settings -> Custom Variables
  • Click Add New Variable after the page loads

magento-2-create-custom-variable-for-email-template

Now enter the following custom variable information.

In the Variable Code field, enter an identifier using all lowercase characters without spaces. You can use an underscore character or hyphen to represent a space. For example: my_custom_variable

Enter a Variable Name, which is used for internal reference. For example: My Custom Variable

To enter the value that is associated with the variable, do one of the following:

For Variable HTML Value, enter the variable value formatted with simple HTML tags. This option allows you to format the value. For example: This content will appear in bold.

For Variable Plain Value, enter the variable value as plain text without formatting. For example: This content will appear as normal text.

Click Save and you are done with creating your custom variable.

add-variable-to-email-template

Step 2: Add Custom Variable to Email Template

Follow these steps to add custom variables to email templates:

  • Navigate to Marketing -> Communications -> Email Templates
  • Choose the template where you want to add the variables to or create a new template.
  • Clicking on the Insert Variable button above the Template Content field will load the list of both predefined and custom variables. Click on any custom variable you want to add to the template and it will be automatically added in the field.

magento-2-custom-variable-template-content

Don’t forget to Save the changes when you are done.

Variables Used in Magento Email Templates

Some of the variables used in magento email templates:


Email Template Variables


VARIABLE MARKUP TAG
Email Logo Image Alt {{var logo_alt}}
Email Logo Image URL {{var logo_url}}
Email Logo Image Height {{var logo_height}}
Email Logo Image Width {{var logo_width}}
Template CSS {{var template_styles|raw}}

New Account Template Variables


VARIABLE MARKUP TAG
Customer Account URL {{var this.getUrl($store, ‘customer/account/’)}}
Customer Email {{var customer.email}}
Customer Name {{var customer.name}}

New Order Template Variables


VARIABLE MARKUP TAG
Billing Address {{var formattedBillingAddress|raw}}
Email Order Note {{var order.getEmailCustomerNote()}}
Order ID {{var order.increment_id}}
Payment Details {{var payment_html|raw}}
Shipping Address {{var formattedShippingAddress|raw}}
Shipping Description {{var order.getShippingDescription()}}

Conclusion

That’s all about adding custom variable to email template in Magento 2. This is as straightforward as it seems. However, no question is a wrong question so don’t hesitate to contact us. For complete list of variables used in magento email templates, check this guide.