How to create a custom variable in Magento

How to Add a Custom Variable in Magento 2

Variables are information that you can use for many pages and used to perform different operations, hold values, and personalize communications. Custom variables can be used in email templates, coding, or WYSIWYG editor.

Related posts: What is Magento?

How do you create a custom variable?

Magento has a list of predefined variables and their values are changeable according to context or programming logic, but sometimes you need to create a custom variable for your business. So you can add a new variable to the list as Custom Variable. For doing that follow the steps:

Step 1: Create a custom variable

Log in to Admin Panel.

Navigate System > Other Settings > Custom Variables.

How to add a custom variable in Magento 2
How to add a custom variable in Magento 2

Click on Add New Variables.

 How to add a Custom Variable in Magento 2
How do you create a custom variable?

To create a Custom Variable, enter the required information.
Variable Code: fill this field with an identifier and use the lowercase characters and an underscore character instead of space. For example, current-year.

Variable Name: fill this field with a name, which is used for internal reference. For example, Current Year.

Variable HTML Value: fill it with a value that is formatted by codes like HTML and JAVA.

Variable Plain Value: You can fill this field with the variable value as plain text without formatting.

Now click on the Save button. You can see your custom variable is created.

 

How to add a custom variable in Magento 2
How to add a custom variable in Magento 2

Step 2: Insert the custom variable

For inserting the variable into a page, navigate Content > Pages.

How to add a custom variable in Magento 2
How do you create a custom variable?


Open the CMS page or block, where the variable is to appear. To do that, click on the Select under Action column and select edit.

How to add a custom variable in Magento 2
How to add a custom variable in Magento 2

Scroll down and expand Content then click on Show/Hide Editor. you can see the code that is selected. 

How to add a custom variable in Magento 2
How to add a custom variable in Magento 2

If you delete this code and save the changes, as you see in the picture below there isn’t any information about the current year to show on the yellow cadre.

How to add a custom variable in Magento 2
How do you create a custom variable?

Return to the backend section of the site and click Show/Hide Editor button, then Insert Variable to insert the custom variable that we have created before.

How to add a custom variable in Magento 2
How to add a custom variable in Magento 2

As you see in the picture Magento arranges variables according to the newest variable. Select the “Custom Variable/ Current Year” and click on Insert Variable.

How to add a custom variable in Magento 2
How to add a custom variable in Magento 2

You see that the new custom variable has been added. To understand better, before this code” Customer code=current year” add a sentence with this content “current year” like the picture below.

How to add a custom variable in Magento 2
How do you create a custom variable?

Now test changes and go to the About Us page and refresh it. You can see the current year in the picture.

How to add a custom variable in Magento 2
How to add a custom variable in Magento 2

 

What are custom variables?

Custom Variables are information that is created once and can be used for different places like email templates, blocks and content pages. There are two kinds of variables, predefined and Custom Variables.

What is a custom variable in Magento 2?

Magento store has a lot of predefined variables that can use them for the personalization of communications. In this article, you can learn the way to define variables. If you want to learn more about defining Custom Variables study the article.

This tutorial is ended. We hope it has been useful and you will be satisfied.