Php 如何使用公共功能前端Magento

Php 如何使用公共功能前端Magento,php,magento,magento-1.7,magento-1.9,magento-1.8,Php,Magento,Magento 1.7,Magento 1.9,Magento 1.8,我在magento中有一个自定义函数,如何在前端查看公共函数值 功能: public function getOptionsWithValues() { $attributes = $item->getOptionByCode('attributes')->getValue(); if (!$attributes) { return null; } $attributes = unserialize($attributes)

我在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;
}

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

  • .phtml文件:从该模板文件,您可以调用任何块函数
如果此函数在块中定义。使用以下代码从phtml文件调用它

$blockObj = $this->getLayout()->getBlock('block_name'); 
调用block函数

echo $blockObj->getOptionsWithValues();

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

$itemOption = Mage::getModel('wishlist/item_option');
如果您在单独的文件中尝试此代码并回显
get\u class($itemOption)
,您将看到类名。现在,您可以通过对象直接访问该函数,如
$itempoption->getOptionsWithValues()


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

您可以在模块的Helper类中包含此代码。然后可以这样调用它:
Mage::helper(“helpername”)->getOptionsWithValues()
我将其添加到core wishlist,/app/code/core/Mage/wishlist/Model/Item/Option.php中,我想在frontendDont hack代码中显示属性值到核心文件中。创建一个模块,将上面的代码插入
Data.php
(这是您模块的助手类)并在前端任意调用它,就像我在第一条评论中向您展示的那样。不要担心这不是问题,我只想告诉我您是否知道如何在前端
echo$options中显示$option['label'][“attribute_name”]
?抱歉,我不明白?wishlist的块名是什么?块名在布局中定义。例如:我们讨论默认的Magento wishlist,为此我请您准确地告诉我块名。