Magento:获取属性代码

Mot*_*ika 0 magento

我试图弄清楚如何从Magento中的过滤器列表中获取属性代码.

<?php
$_filters = $this->getFilters();
foreach ($_filters as $_filter)
{
    echo $this->__($_filter->getName());
    echo $this->__($_filter->getAttributeCode()); # color_name
}
?>
Run Code Online (Sandbox Code Playgroud)

getAttributeCode()不是一个方法.我想为app/design/frontend/default/default/template/catalog/layer/view.phtml中的attribute_code指定每个过滤器的CSS类名

小智 14

以下将有效:

foreach($filters as $_filter)
{
    $attributeModel = $_filter->getAttributeModel();
    if($attributeModel) {
        echo $attributeModel->getAttributeCode();
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的关键是检查过滤器实际上是一个属性,因为有些不是(最常见的类别),这些类型的过滤器显然不具有属性代码.