Magento 2: Add Custom Variable to Email Template

Magento 2: Add Custom Variable to Email Template

The concept of custom variables is not new. They are used from time immemorial in every organization and programming language. They are pieces of information used to perform different operations, hold values, and personalize 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.

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


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.


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.


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

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

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

New Order Template Variables

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()}}
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.