Magento: how do I access custom variables in PHP?

前端 未结 6 894
清歌不尽
清歌不尽 2020-12-23 19:18

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 templa

相关标签:
6条回答
  • 2020-12-23 19:41

    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 讨论(0)
  • 2020-12-23 19:44

    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.

    0 讨论(0)
  • 2020-12-23 19:46

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

    0 讨论(0)
  • 2020-12-23 19:47

    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 讨论(0)
  • 2020-12-23 19:57

    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.

    0 讨论(0)
  • 2020-12-23 20:01

    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');
    
    0 讨论(0)
提交回复
热议问题