开发者

Magento: how do I access custom variables in PHP?

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


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.


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.


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');


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


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


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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜