Magento从filter.phtml获取属性代码


Magento get attribute code from filter.phtml

我正在尝试从filter.phtml模板文件中获取属性代码。有人知道如何做到这一点吗?

$_item->getFilter()->getAttributeModel()->getAttributeCode()

上面的代码行可用于在循环中获取属性代码。

只有当过滤器是类别的属性 bu 时,它才会起作用,它不起作用所以你必须写成

echo $_filter->getType();
if($_filter->getType()=='catalog/layer_filter_attribute')
{
    echo $_filter->getAttributeModel()->getAttributeCode();
}

基于@Darren答案@Posuk13注释,但要在 items 循环之外获取属性代码,您可以使用:

$this->getFilter()->getRequestVar()

(在Magento 1.9社区版上测试)

特别是在 filter.phtml 中使用的类$this上下文中,以下打印/返回属性代码:-

<?php echo $this->getAttributeModel()->getAttributeCode() ?>