【问题标题】:Magento: how do I access custom variables in PHP?Magento:如何在 PHP 中访问自定义变量?
【发布时间】:2011-06-02 22:20:29
【问题描述】:

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

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

我希望能够访问“可变纯值”以检索转换值,即数字/字符串作为给定“可变代码”的数字。

【问题讨论】:

    标签: magento


    【解决方案1】:

    一段时间以来,我一直在这样做以创建可通过管理界面编辑的各种消息,这样当当下的风格发生变化时,我就不必去挖掘代码了。

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

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

    注意:单一商店不显示变量范围的商店选择下拉菜单。这个答案在技术上是不正确的,为了在拥有多家商店的情况下让自己在未来得到证明 --> 请参阅下面的@Mark van der Sanden 答案并给他一个支持。

    【讨论】:

    • 没有成功,但这样做了:$angle = Mage::getModel('core/variable')->loadByCode('angle')->getData('store_plain_value');
    • 从纯数据中尝试 getValue('text') 并为 html 尝试 getValue('html')
    • @mark-van-der-sanden answer 更正确,因为尊重网站/视图重载的值
    • 这个答案对于 1.5.1.0 和以前的版本似乎是正确的。据我所知,这些变量对于 Magento 安装来说是全局的,并且没有任何方法可以设置存储范围。
    【解决方案2】:

    很遗憾,所有其他答案都不是 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 值,getValue() 在您请求 html 值时返回文本值。

    【讨论】:

    • 如何为这些变量设置存储范围?这是新版本中的东西吗?我使用的是 1.5.1.0,系统配置下拉菜单中只有一个菜单项,您可以在其中添加这些项,因此它们似乎是全局项。
    • 保存自定义变量后,您可以使用表单上方的下拉菜单选择商店视图。然后,您可以选择是要使用默认值还是指定存储特定值。所以只有你保存了变量之后,你才能输入存储特定的值。
    • 刚刚签入 CE 1.5.0.1 并且商店下拉菜单也在那里(确保您定义了多个商店!)。
    • 好的,我只有一个商店,商店下拉菜单从未显示。
    • 在这种情况下,暂时不需要使用setStoreId(),但最好还是这样做以防万一。
    【解决方案3】:

    Stackoverflow 几乎又来了。以为是这样:

    Setting a global variable in Magento, the GUI way?

    但不是,这是:

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

    【讨论】:

    • @mark-van-der-sanden answer 更正确,因为尊重网站/视图重载的值
    【解决方案4】:

    我认为您可以通过在模板块中使用方法来实现这一点的唯一方法,该方法将输出所需的结果。

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

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

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

    为了澄清起见,块是变量 $this

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

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

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

    这是最好的方法。

    HTH :)

    【讨论】:

      【解决方案5】:

      // 获取自定义变量的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设置为自定义变量,可编辑多个店铺

      【讨论】:

        【解决方案6】:

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

        所以要使用代码 custom_variable_code

        访问自定义变量的 store specific value

        使用这个:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多