19

I am aware of 'Custom Variables' and how they can be used with {{ }} brackets in email templates as well as in static blocks.

However, I want to use them in template code i.e. view.phtml.

I want to be able to access 'variable plain value' to retrieve a conversion value, i.e. a number/string as number for a given 'variable code'.

6 Answers 6

31

Been doing this for some time to create various messages that are editable through the admin interface so I don't have to go code digging when the flavor of the moment changes.

To access the plain value of the custom variable with code custom_variable_code use this:

Mage::getModel('core/variable')->loadByCode('custom_variable_code')->getValue('plain');

NOTE: Single store doesn't show the store select dropdown for the variable scope. This answer is not technically correct, in order to future-proof yourself in case of having multiple stores --> Please see @Mark van der Sanden answer below and give him an upvote.

4
  • 1
    Didn't work out, but this did: $angle = Mage::getModel('core/variable')->loadByCode('angle')->getData('store_plain_value'); Commented Jun 9, 2011 at 0:38
  • 1
    try getValue('text') from plain data and getValue('html') for html Commented Oct 14, 2012 at 23:03
  • 2
    @mark-van-der-sanden answer is more correct since respects website/view overload of the value
    – lrkwz
    Commented Nov 7, 2013 at 11:35
  • This answer seemed to be correct for 1.5.1.0 and previous. From what I could see, the variables are global to the Magento installation and there isn't any way of setting store scope. Commented Nov 7, 2013 at 15:57
31

Unfortunately, all other answers are not 100% correct. Use it like this (note the setStoreId() to get the value for the correct store view):

$value = Mage::getModel('core/variable')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->loadByCode('variable_code')
    ->getValue('text');

Or to get the html value:

$value = Mage::getModel('core/variable')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->loadByCode('variable_code')
    ->getValue('html');

If no html value is defined, getValue() returns the text value if you request the html value.

5
  • How is the store scope set for these variables? Is this something in a newer version? I'm using 1.5.1.0 and there's just a menu item in the System Config dropdown where you add these so they seem to be global items. Commented Nov 7, 2013 at 15:56
  • 2
    Once you saved the custom variable, you can choose a store view using the dropdown above the form. You can then choose whether you want to use the default values, or specifiy store specific values. So only after you saved the variable, you can enter store specific values. Commented Nov 8, 2013 at 7:51
  • Just checked in CE 1.5.0.1 and the store dropdown is there too (make sure you have multiple stores defined!). Commented Nov 8, 2013 at 7:57
  • 1
    Ok, I have only a single store, the Store Dropdown never showed. Commented Nov 11, 2013 at 6:54
  • In that case there's no need to use setStoreId() for now, but it's definitely better to do so to be future proof. Commented Nov 11, 2013 at 10:18
10

Stackoverflow almost to the rescue again. Thought this would be it:

Setting a global variable in Magento, the GUI way?

But it wasn't, this was:

  $angle = Mage::getModel('core/variable')->loadByCode('angle')->getData('store_plain_value');
1
  • 2
    @mark-van-der-sanden answer is more correct since respects website/view overload of the value
    – lrkwz
    Commented Nov 7, 2013 at 11:34
1

The only way I see you can acheive this by having a method in the templates block, that will output the needed result.

For instance say in the template view.phtml you have the following code:

<div id="title_container">
    <h2><?= $this->getTitle(); ?></h2>
</div>

The function can represent your variable code and any logic that has to do with what gets displayed in the title should be placed in the block.

Just for clarification sake the block is the variable $this

If you are unsure what is the actual class name of your block you can do something like:

Mage::log(get_class($this));

in the var/log/system.log you will print the class of the block of that template.

That is the best way.

HTH :)

0
1

// To get the TEXT value of the custom variable:

Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('custom_variable_code')->getValue('text');

// To get the HTML value of the custom variable:

Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('custom_variable_code')->getValue('html');

// The store id is set as Custom Variables can be edited for multiple stores

1

Note: A custom variable might have different values for different stores.

So to access store specific value for the custom variable with the code custom_variable_code

Use this:

$storeId = Mage::app()->getStore()->getId();

  $custom_variable_text = Mage::getModel('core/variable')->setStoreId($storeId)
                          ->loadByCode('custom_variable_code')
                          ->getValue('text');

  $custom_variable_plain_value = Mage::getModel('core/variable')->setStoreId($storeId)
                                ->loadByCode('custom_variable_code')
                                ->getValue('plain');

  $custom_variable_html_value = Mage::getModel('core/variable')->setStoreId($storeId)
                                ->loadByCode('custom_variable_code')
                                ->getValue('html');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.