2010-02-06 102 views
0

我想弄清楚如何從Magento的過濾器列表中獲取屬性代碼。Magento:獲取屬性代碼

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

getAttributeCode()不是一種方法。 我想指定每個過濾器在應用程序/設計/前端/預設/默認/模板/目錄/層的attribute_code一個CSS類名/ view.phtml

回答

14

下面的工作:

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

這裏的關鍵是檢查過濾器實際上是一個屬性,因爲有些不是(最常見的類別),並且這些類型的過濾器顯然不會有屬性代碼。

+0

非常感謝。 () $ this - > _ getFilterableAttributes() 函數getFilters()在app/code/core/Mage/Catalog/Block/Layer/View.php中: $ filters [] = $ this- > getChild($屬性 - > getAttributeCode().'_濾波器'); – MotionGrafika 2010-02-09 04:36:11

3

注意您是否要在/catalog/layered/state.phtml中使用此代碼段;請使用

$attributeModel = $_filter->getFilter()->getAttributeModel(); 

,而不是

$attributeModel = $_filter->getAttributeModel();