【问题标题】:How to use public function frontend Magento如何使用公共功能前端 Magento
【发布时间】:2016-08-20 04:37:15
【问题描述】:

我在 magento 中有一个自定义函数,如何在前端查看公共函数值?

功能:

public function getOptionsWithValues()
{
    $attributes = $item->getOptionByCode('attributes')->getValue();

    if (!$attributes)
    {
        return null;
    }

    $attributes = unserialize($attributes);
    $options = array();

    foreach ($attributes as $attr_id => $attr_value)
    {
        $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attr_id);

        $attr_options = $attribute->getSource()->getAllOptions(false);   

        foreach ($attr_options as $option)
        {
            if ($option['value'] == $attr_value)
            {
                $options[$attribute->getFrontendLabel()] = $option['label'];
            }
        }
    }

    return $options;
}

谢谢

【问题讨论】:

  • 您可以在模块的 Helper 类中拥有此代码。然后你可以这样称呼它:Mage::helper("helpername")->getOptionsWithValues();
  • 我在核心愿望清单中添加了这个,/app/code/core/Mage/Wishlist/Model/Item/Option.php 我想在前端显示属性值
  • 不要将代码破解到核心文件中。创建一个模块,将上面的代码插入到Data.php(这是您模块的帮助程序类)中,然后在前端任何您想要的地方调用它,就像我在第一条评论中向您展示的那样。
  • 别担心这不是问题,我只想告诉我你是否知道如何在前端显示 $option['label']
  • echo $options["attribute_name"] ?

标签: php magento magento-1.7 magento-1.9 magento-1.8


【解决方案1】:

正如您所说,您在 /app/code/core/Mage/Wishlist/Model/Item/Option.php 中添加了此代码。所以你可以使用工厂方法来实例化这个类,比如

$itemOption = Mage::getModel('wishlist/item_option');

如果您在单独的文件中尝试此代码并回显get_class($itemOption),您将看到类名。现在你可以通过$itemOption -> getOptionsWithValues()这样的对象直接访问函数了。

但是您永远不应该直接在核心文件中进行更改,而是可以在本地文件夹中复制相同的文件夹结构或重写您想要覆盖的模型类。

【讨论】:

    【解决方案2】:

    在 Magento 中,如果您想通过调用函数来显示某些值。您需要在块中定义该函数

    • .phtml 文件:从这个模板文件你可以调用任何块函数。

    如果这个函数是在一个块中定义的。使用此代码从 phtml 文件中调用它。

    $blockObj = $this->getLayout()->getBlock('block_name'); 
    

    调用块函数

    echo $blockObj->getOptionsWithValues();
    

    【讨论】:

    • 抱歉,我不明白?愿望清单的区块名称是什么?
    • 块名在布局中定义。示例:
    • 我们讨论的是默认的 Magento 愿望清单,为此我要求您准确地告诉我块名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 2019-12-11
    • 2012-02-27
    相关资源
    最近更新 更多