Some site Administrators are surprised to find out that Magento comes with several variables that allow users to plug in values that are commonly used. For instance, if you were to use a specific email address in the content of several of your pages, it would make sense to use a variable. That way should the email address ever change, you would only have to update it in one place instead of each page.

Similarly, if you use your store information (such as the telephone number or address) in more than one area on your site, then it makes sense to use a variable should any of that information change.

How to Use a Variable

Using a variable is simple when editing pages or static blocks.

Using a Variable in the WYSIWYG Editor

If you are using the WYSIWYG editor, you should see an orange icon that looks like this: {{v}} on the top left of the icon set. See the screenshot below.

By clicking this icon, a popup will appear with the following options:

  • Base Unsecure URL
  • Base Secure URL
  • General Contact Name
  • General Contact Email
  • Sales Representative Contact Name
  • Sales Representative Contact Email
  • Custom1 Contact Name
  • Custom1 Contact Email
  • Custom2 Contact Name
  • Custom2 Contact Email
  • Store Name
  • Store Contact Telephone
  • Store Contact Address

These are the default Magento Variable options. The final list can vary from store to store, as you may have some custom variables included.

Note: Don’t be alarmed when using a variable; the WYSIWYG editor cannot translate the variable in the preview for you, so you will see a code similar to the one below. Magento will automatically fill in the correct information when it sees that code.

{{config path="trans_email/ident_general/name"}}

Using a Variable in the Code Editor

Using a variable in the code editor without the visual WYSIWYG editor is simple. You should see an orange ‘Insert Variable’ button on the top right. By clicking this button, you get the same list of variables as above.

Your variable will look similar to the code below.

{{config path="trans_email/ident_general/name"}}

Default Magento Variables

Base URL variables

Magento has variables that dynamically grab the Base URL of your site. For instance, if you had the domain name example.com it would prefill in the http://example.com/ when using that variable. This can be useful in many different scenarios.

If you ever change the domain name

Say you wanted to change the domain name to exampleparts.com instead. Since you had already been using the Base URL variable, it will automatically select the new domain once it is set up in System Configuration. If you have multiple stores set up and you are editing an area that shows on all those stores, using a Base URL variable will keep track of the correct domain for each store.

If you are using multiple environments

In an ideal website setup, you’ll have three separate site environments for you website. You’ll have your production site on your main domain, as well as a staging site for testing that is on a different domain, and lastly, your developers or people working on your site will have a copy on their computer to work on.

All three environments will likely have different domains. So if you are using hard links (manually typing in the domain in the URL) then all the links will be broken on your stage and developer environments. Using the Base URL variable saves the hassle of having those broken links in different environments.

Using Store information Variables

Similar to using the Base URL variable, there are variables set up for store address as well as telephone number.

The data is stored in your Magento Admin under System > Configuration > General > General > Store Information.

These values can be changed based on what store view or website that you have them filled out for. For instance, if you display your store information, telephone and address in the footer, you can use these variables instead of manually typing in the information. Then, if that footer is displayed on multiple stores, it will display the correct information for that site. So you could have an English store view showing one phone number and address, and your German store view showing a different address and phone number.

To do this, you will change your ‘Configuration Scope’ on the top left of the page to match the configuration of the website or store view you want to edit. This will allow you to update the values for that website or store view’s configuration.

These can then be selected in the variable list when editing a page or CMS block.

Using Email Variables

Magento also comes with some email variables that are used throughout the Admin. These can be edited in the Admin under System > Configuration > General > Store Email Addresses.

This section lets you edit the email addresses for your general contact, sales representative, customer support, as well as two other custom email addresses.

The email address and/or name are also available in the variable drop down when you are editing a page or CMS block.

Just like the store information, you can set different information for each website and store view by changing the ‘Configuration Scope’ at the top left of the page.

Custom Variables

On top of the variables that come with Magento, there are also custom variables that you can set up to use. These can be useful for adding extra pieces of information that you want to use across your site. Maybe a fax number, another email address, or a title.

You can create custom variables in your Admin under System > Custom Variables. This will give you a list of any current custom variables you have already created, as well as the option to create new ones. When you create a new variable you will be asked for four types of information:

  • Variable Code - the code that Magento stores in the database. No spaces.
  • The Variable Name - this is the title you will see in the Admin when selecting this variable.
  • Variable HTML Value - this is HTML code that will be stored in the variable
      Use the HTML value if you are wanting something displayed with a specific format, for instance an address on multiple lines or an item with a specific class attached to it. Another common use for the HTML value is to hold a really small snippet of code that you want to use multiple times on the site.
  • Variable Plain Value - this is just the value in plain text
      If you are wanting something simple to be displayed, like an email address, telephone number, name, title or even fax number.

Now your custom variable will be shown in the list of variables when editing a page or CMS block as seen in the screenshot below.

Magento variables can be useful for your site. While they may be a little tricky to figure out, they work really well, and can be very helpful in certain situations.