Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: how do I access custom variables in PHP?

Tags:

magento

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

like image 246
ʍǝɥʇɐɯ Avatar asked Jun 02 '11 22:06

ʍǝɥʇɐɯ


6 Answers

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.

like image 185
Fiasco Labs Avatar answered Oct 13 '22 00:10

Fiasco Labs


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.

like image 20
Mark van der Sanden Avatar answered Oct 13 '22 01:10

Mark van der Sanden


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');
like image 26
ʍǝɥʇɐɯ Avatar answered Oct 12 '22 23:10

ʍǝɥʇɐɯ


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 :)

like image 44
Gabriel Spiteri Avatar answered Oct 13 '22 01:10

Gabriel Spiteri


// 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

like image 34
gaurangkathiriya Avatar answered Oct 13 '22 01:10

gaurangkathiriya


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');
like image 34
Rahul Gupta Avatar answered Oct 13 '22 01:10

Rahul Gupta