6

我正在尝试列出 magento 1.7.0.2 中新创建的属性的所有现有值。(并使它们成为可点击的链接,以便在单击时列出具有特定属性值的所有项目,但这不是现在的优先事项)

属性代码是“艺术家”

到目前为止,我使用以下代码在 app/code/core/Mage/Catalog/Block/ 中创建了文件 Artist.php:

public function getAllArtists()
{
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
    ->setEntityTypeFilter($product->getResource()->getTypeId())
    ->addFieldToFilter('attribute_code', 'artist');
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
$artists = $attribute->getSource()->getAllOptions(false);
  return $artists;  
}

和 /app/design/frontend/default/template-name/template/catalog/product 中的文件 artist.phtml 使用以下代码:

 <ul id="artist_list">
  <?php foreach ($this->getAllArtists() as $artist): ?> 
  <li><a href="<?php Mage::getURL() ?>catalogsearch/advanced/result/?&artist;[]=<?php echo $artist['value'] ?>&search;="><?php echo $artist['label'] ?></a></li>
  <?php endforeach; ?>
 </ul>

然后我在一个静态块中调用

{{block type="core/template" template="catalog/product/artist.phtml"}}

但什么都没有出现...

我使用了这个线程的代码:http: //www.magentocommerce.com/boards/viewthread/19982/P0/

该属性设置为“在前端的产品视图页面上可见”,我将 ../template/product/view.phtml 中每个项目的属性值称为

<?php echo $_product->getData('artist') ?> 

并正确显示该值。

有任何想法吗?

4

2 回答 2

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

参考:Magento - 获取所有属性值

于 2013-10-24T11:37:21.630 回答
1

这将只是

$attribute = Mage::getModel('eav/config')->getAttribute(4,'artist');
if($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
    foreach($options as $key=>$value) {
        if(count($value)==2) {
            $artists[] = $value['label'];
        }
    }
}
于 2013-03-19T20:25:30.450 回答