tags:

views:

443

answers:

1

I am trying to figure out how to get an attribute code from a filter list in Magento.

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

getAttributeCode() isnt a method. I want to specify a CSS classname for each filter to the *attribute_code* in app/design/frontend/default/default/template/catalog/layer/view.phtml

+1  A: 

The following will work:

foreach($filters as $_filter)
{
    $attributeModel = $_filter->getAttributeModel();
    if($attributeModel) {
        echo $attributeModel->getAttributeCode();
    }
}

The key here is to check that the filter is actually an attribute as some aren't (most commonly categories) and these types of filters obviously won't have an attribute code.

Chris Norton
Thanks a lot.I was nearly there by getting it from $this->_getFilterableAttributes()Function getFilters() in app/code/core/Mage/Catalog/Block/Layer/View.php : $filters[] = $this->getChild($attribute->getAttributeCode().'_filter');
MotionGrafika