2014-01-07 76 views
1

我是Magento的新手,我正在建造一家書店。我有一個稱爲作者的屬性,我想顯示所有作者的列表(它們的屬性值列表)。我試圖創建一個小部件,並使用它的代碼,但它返回一個空數組。我怎樣才能做到這一點?我應該在哪裏放置代碼,在一個小部件,一個塊?Magento:獲取屬性值列表

protected function _toHtml() 
{ 

    $name='author'; 
    $attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($name)->getFirstItem(); 
    $attributeId = $attributeInfo->getAttributeId(); 
    $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId); 
    $attributeOptions = $attributeInfo->getSource()->getAllOptions(false); 

    $html = '<ul>'; 
    foreach($attributeOptions as $opt) 
     $html .= '<li>'.$opt[0].'</li>'; 
    $html .= '</ul>'; 

    return $html; 
} 

回答

0
$attributes = $product->getAttributes(); 
foreach ($attributes as $attribute) { 
    if ($attribute->getIsVisibleOnFront()) { 
     $value = $attribute->getFrontend()->getValue($product); 
     echo $value 
    } 
} 

你可以用這個代碼屬性只是寫在view.phtml

0

這個代碼非常感謝你,你的代碼工作的特定產品相當不錯的,但是我終於得到我想要使用這個:

$attributeCode='author'; 
// build and filter the product collection 
$products = Mage::getResourceModel('catalog/product_collection') 
    ->addAttributeToFilter($attributeCode, array('notnull' => true)) 
->addAttributeToFilter($attributeCode, array('neq' => '')) 
->addAttributeToSelect($attributeCode); 

// get all distinct attribute values 
$usedAttributeValues = array_unique($products->getColumnValues($attributeCode)); 
sort($usedAttributeValues);