需要帮助了解productAttribute()产品页面的方法

MSD*_*MSD 16 php methods helper magento

我需要帮助熟悉帮助者,他们的方法和产品属性.特别:$_helper->productAttribute($product, $attributeHtml, $attributeName)

以下是我正在使用/审核的文件:

app\code\core\Mage\catalog\helper\output.php  
app\design\frontend\[theme name]\template\catalog\product\view\media.phtml
Run Code Online (Sandbox Code Playgroud)

以下代码行生成产品图像的html.

echo $_helper->productAttribute($_product, $_img, 'image');
Run Code Online (Sandbox Code Playgroud)

辅助类代码描述了以下代码段中的方法.返回什么,步骤是什么,为什么我会使用这种方法而不是简单地回显模板文件前一行中描述的img html?

public function getHandlers($method)
{
    $method = strtolower($method);
    return isset($this->_handlers[$method]) ? $this->_handlers[$method] : array();
}

public function process($method, $result, $params)
{
    foreach ($this->getHandlers($method) as $handler) {
        if (method_exists($handler, $method)) {
            $result = $handler->$method($this, $result, $params);
        }
    }
    return $result;
}

public function productAttribute($product, $attributeHtml, $attributeName)
{
    /* Code that is not relevant to this example */

    $attributeHtml = $this->process('productAttribute', $attributeHtml, array(
        'product'   => $product,
        'attribute' => $attributeName
    ));

    return $attributeHtml;
}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

Iva*_*nyi 29

非常好的问题!

所以实际上有点关于这个助手的目的.从它的名字你可以得出它用于输出数据的结论.方法名称也是自解释的,它只是输出产品属性值取决于处理程序.目前有两种方法,productAttribute()用于输出产品属性值categoryAttribute(),用于类别.来自类别和产品的核心模板中的所有数据都通过此方法输出(价格属性除外),据我记得它是在1.4.x版本中添加的,但不确定.主要思想是可以过滤属性的数据.例如,您可以{{widget ... }}在类别描述中使用构造,它通过特殊方法实现.

这两种方法实际上都具有相同的功能,但适用于不同的实体.他们都收到3个论点:

  • 实体(类别或产品,取决于方法名称)
  • 属性值 - 过滤的值
  • 属性代码 - 用于检索属性模型的代码

首先在这个方法中,Magento检查值中是否允许html标记,如果没有,它会使用escapeHtml()方法转义文本.此外,如果属性在admin中具有textarea作为输入,则所有新行字符都将替换为<br />tag.

如果允许html,Magento会检查{{widget ...}}配置中的特殊构造的容差(此构造的官方名称是指令).如果允许指令,则会对特殊指令处理器进行实例化并处理值.

完成所有核心处理后,Magento会调用处理程序.

此处理程序是核心模块未使用的其他功能,但您可以使用自己的自定义来实现一些不错的自定义.下面是示例:您希望以大写形式生成产品名称的所有输出.然后,您可以添加自己的处理程序,为此,请按照以下简单步骤操作:

  1. 定义一个观察者 catalog_helper_output_construct

    <config>
       <frontend>
           <events>
               <catalog_helper_output_construct>
                   <observers>
                        <your_module>
                            <class>your_module/observer</class>
                            <method>handleHelperOutputInitialization</method>
                        </your_module>
                   </observers>
               </catalog_helper_output_construct>
           </events>
       </frontend>
    </config>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建你的观察者类,我也将它作为处理程序.代码很简单:

    class Your_Module_Model_Observer 
    {
        public function handleHelperOutputInitialization($observer) 
        {
            $helper = $observer->getEvent()->getHelper();
            $helper->addHandler('productAttribute', $this);
        }
    
       public function productAttribute($helper, $value, $parameters) 
       {
            $attribute = $parameters['attribute'];
            if ($attribute->getAttributeCode() == 'name') {
               return strtoupper($value);
            }
            return $value;
       }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 确保处理程序类中的方法名称与值处理器的方法名称完全相同,在此示例中为productAttribute().

享受学习Magento!

  • @MSD你总是可以接受一个答案,如果你也不能赞成它.如果能帮助您解决问题,请考虑接受答案. (2认同)