【问题标题】:Magento: get a list of attribute valuesMagento:获取属性值列表
【发布时间】:2014-01-07 10:48:16
【问题描述】:

我是 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;
}

【问题讨论】:

    标签: list magento attributes widget


    【解决方案1】:
    $attributes = $product->getAttributes();
    foreach ($attributes as $attribute) {
        if ($attribute->getIsVisibleOnFront()) {
            $value = $attribute->getFrontend()->getValue($product);
            echo $value
        }
    }
    

    您可以使用此代码获取属性,只需将此代码写入view.phtml

    【讨论】:

      【解决方案2】:

      非常感谢,您的代码对特定产品非常有效,但是我终于得到了我想要的东西:

      $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);
      

      【讨论】:

        猜你喜欢
        • 2014-04-27
        • 2011-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多