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'.

ʍǝɥʇɐɯ
  • 4,012
  • 7
  • 32
  • 53

6 Answers6

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.

  • 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. – Fiasco Labs Nov 07 '13 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. – Mark van der Sanden Nov 08 '13 at 07:51
  • Just checked in CE 1.5.0.1 and the store dropdown is there too (make sure you have multiple stores defined!). – Mark van der Sanden Nov 08 '13 at 07:57
  • 1
    Ok, I have only a single store, the Store Dropdown never showed. – Fiasco Labs Nov 11 '13 at 06: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. – Mark van der Sanden Nov 11 '13 at 10:18
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.

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
Fiasco Labs
  • 6,457
  • 3
  • 32
  • 43
  • 1
    Didn't work out, but this did: $angle = Mage::getModel('core/variable')->loadByCode('angle')->getData('store_plain_value'); – ʍǝɥʇɐɯ Jun 09 '11 at 00:38
  • 1
    try getValue('text') from plain data and getValue('html') for html – Ricky Sharma Oct 14 '12 at 23:03
  • 2
    @mark-van-der-sanden [answer](http://stackoverflow.com/a/18003432/509565) is more correct since respects website/view overload of the value – lrkwz Nov 07 '13 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. – Fiasco Labs Nov 07 '13 at 15:57
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');
Community
  • 1
  • 1
ʍǝɥʇɐɯ
  • 4,012
  • 7
  • 32
  • 53
  • 2
    @mark-van-der-sanden [answer](http://stackoverflow.com/a/18003432/509565) is more correct since respects website/view overload of the value – lrkwz Nov 07 '13 at 11:34
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');
Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
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 :)

Gabriel Spiteri
  • 4,896
  • 12
  • 43
  • 58