Magento 2

How to add custom variables in Magento 2

February 11, 2022

How to add custom variables in Magento 2

In this article we will learn how to add custom variables in Magento 2, which are variables that we can reuse in various places as modules and templates.

add custom variables in Magento 2 cover

What’s up programmer, ok? Let’s learn about custom variables in Magento

Magento 2 custom variables or custom variables can be easily created

And it has a wide range of uses, as we can use it from CMS blocks to templates or modules

And as I said, to create it is very simple, in the administrative area go to: System | Other Settings | Custom Variables

Then click on Add New Variable and you will get the following screen:

Here we must fill in the Variable Code in the way that we expect to call this variable in places

Set up a name in Variable Name

And the value fields will be the values displayed in the different places that we will use the variable

|   Do you want to specialize in Web Development? Take a look at our course catalog.

If you are going to use it on a CMS page, for example, you should call it like this:

{{CustomVar code="custom_var"}}

Where custom_var is the Variable Code you filled in

If you are going to call the variable in the template, the right thing would be to create a block (Block) with depency injection

And then call the variable in the template through $this, as it would already be ‘injected’ in the template

obs: Using _objectManager goes against the principles of Magento

Conclusion

In this article we learned  how to add custom variables, the famous custom variables

And how to use it in the CMS: pages, blocks, emails, etc.

To use it on the front (.phtml), it would be necessary to create a Block and make a depency injection

Which is a little off topic, but could be a new article

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x