Magento Custom Variables

July 12, 2016

Image for Magento Custom Variables from unsplash.com

Magento provides an interface for you to define your own custom variables. You can use these in CMS pages, static blocks, email templates and phtml templates. Here's how to use them.

Defining Custom Variables

You can add custom variables in the admin area. Go to 'System > Custom Variables'.

For example, we're going to create a custom variable to output a link to this blog. Fill in the fields as follows:

Defining Magento Custom Variables

Save this.

Using Custom Variables in CMS page and Static Blocks

To use our new custom variable in a CMS page or a static block we add the following markup:

{{ customVar code=euperia_blog_link }}

Using Custom Variables in PHTML Templates

To use the custom variable in a phtml template we need to load it, specifically defining the HTML or Plain value:

    $customHtml = Mage::getModel('core/variable')
        ->loadByCode('euperia_blog_link')->getValue('html');
    $customPlain = Mage::getModel('core/variable')
        ->loadByCode('euperia_blog_link')->getValue('plain');

    echo $customHtml;
    echo $customPlain;