How to Add Custom Config Variable

Happy New Year! This year we continue our journey on how to do things in Magento 2!As we know already Magento provide us several predefined but also custom variables that we can create and use in email templates or content elements. In this post we will see how we can add a custom variable related to a configuration value.

First of all we will add a configuration adding an addition store info like customer support email address under Stores -> Configuration -> General -> Store Information.

So we have to a system.xml file and also a config.xml for setting a default value.

So the result in admin area will be like below

Now to add this configuration value as variable available, we will need to add it as an item to the item general/store_information of the argument configPaths that is passed to the class Magento\Variable\Model\Config\Structure\AvailableVariables in the di.xml file.

In admin area now our new variable will be available in the General / Store Information section of the variables.

You can also add it directly in email templates or content elements adding the line

The full example as extension can be found here.

Feel free to share this post and ask your questions in the comments below.

Till next time!

DigitalOcean Referral Badge