Magento:我如何在PHP中访问自定义变量?

时间:2011-06-02 22:20:30

标签: magento

我知道“自定义变量”以及它们如何与电子邮件模板以及静态块中的{{}}括号一起使用。

但是,我想在模板代码中使用它们,即view.phtml。

我希望能够访问'变量普通值'来检索转换值,即数字/字符串作为给定'变量代码'的数字。

6 个答案:

答案 0 :(得分:31)

已经这样做一段时间来创建可通过管理界面编辑的各种消息,所以当时刻的味道发生变化时,我不必去挖掘代码。

要使用代码 custom_variable_code 访问自定义变量的普通值,请使用:

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

注意:单个商店不显示变量范围的商店选择下拉列表。这个答案在技术上是不正确的,以便在有多个商店的情况下为自己提供未来证明 - >请参阅下面的@Mark van der Sanden回答并给他一个upvote。

答案 1 :(得分:30)

不幸的是,所有其他答案都不是100%正确。像这样使用它(注意setStoreId()以获取正确的商店视图的值):

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

或者获取html值:

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

如果没有定义html值,如果您请求html值,getValue()将返回文本值。

答案 2 :(得分:10)

Stackoverflow几乎再次救援。以为这就是它:

Setting a global variable in Magento, the GUI way?

但事实并非如此:

  $angle = Mage::getModel('core/variable')->loadByCode('angle')->getData('store_plain_value');

答案 3 :(得分:1)

我看到你的唯一方法是通过在模板块中使用一个方法来实现这一点,这将输出所需的结果。

例如,在模板view.phtml中,您有以下代码:

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

该函数可以表示您的变量代码,任何与标题中显示的内容有关的逻辑都应该放在块中。

为了澄清,块是变量$ this

如果您不确定块的实际类名是什么,您可以执行以下操作:

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

在var / log / system.log中,您将打印该模板块的类。

这是最好的方法。

HTH:)

答案 4 :(得分:1)

//获取自定义变量的TEXT值:

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

//获取自定义变量的HTML值:

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

//商店ID设置为可以为多个商店编辑自定义变量

答案 5 :(得分:0)

注意:自定义变量可能具有不同商店的不同值。

因此,要使用代码 custom_variable_code

访问自定义变量的商店特定值

使用此:

$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');
相关问题