JoomlaVision > Magento > Magento Custom Variables

Magento Custom Variables

Custom variables in Magento have been around since the launch of 1.4, but they have not really been given much attention in that time – probably because they’re not really an essential part of Magento. Or it could be that they’re hidden away in the System menu like a forgotten concept from yesteryear.

What’s the purpose of custom variables?

If you do any sort of coding you know that all data should be stored in variables, so that it can be reused at any time and if it needs changing it only needs doing once. Magento had introduced this mainly for email templates, but it can be used in template files too (though the functionality is very similar to static blocks). The difference between them and static blocks though is the ability to enter data as plain text – which could prove much less destructive if left in the hands of clients.

Simply go to System Custom Variables and then click ‘Add New’.

In this example I’ll create a variable called ‘opening_hours’ and I’m going to add content into the HTML textarea. Once you’ve saved the variable, if you have a multi-store setup you’ll be able to define different content for each store which is incredibly helpful if your stores use different languages.

Transactional Emails

The variable can now be displayed using the transactional email shortcode:

{{customVar code=opening_hours}}

Template (.phtml) Files

In a similar fashion to static blocks, your variable can also be displayed with a simple PHP callout:

HTML Value:

Mage::getModel('core/variable')-loadByCode('opening_hours')-getValue('html')

Text Value:

Mage::getModel('core/variable')-loadByCode('opening_hours')-getValue('text')

 

Related posts