如何使用公共函数前端Magento


How to use public function frontend Magento

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

谢谢

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

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

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

但是,切勿直接在核心文件中进行更改,而是可以复制本地文件夹中的相同文件夹结构或重写要重写的模型类。

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

  • .phtml 文件 :从此模板文件中,您可以调用任何块函数。

如果此函数是在块中定义的,则使用此代码从 PHTML 文件调用它。

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

调用块函数

echo $blockObj->getOptionsWithValues();