获取magento中所有产品属性的数组

时间:2011-02-10 18:29:27

标签: php zend-framework magento magento-1.4

我无法弄清楚这一点!

我正在尝试将list.phtml页面上的产品属性列表添加到数组中。我尝试了一切。我见过很多使用

的解决方案
$attributes = $product->getAttributes();

但我不能让它工作,它只是打开一个空白页面。任何帮助将不胜感激,到目前为止,我花了数小时和数小时......

我正在使用Magento版本1.4.2.0

更新:这正是我想要做的事情:

$neededAttributes = Mage::helper('mymodule')->getNeededAttributes();
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
   if(in_array($attribute->getAttributeCode(), $neededAttributes)) { 
      $attributename = $attribute->getAttributeCode();
  echo $attributename;
   }
 }

这是在design / adminhtml / default / default / catalog / product / helper /

中的gallery.phtml文件中

由于某种原因,我无法让getAttributeCode函数返回任何内容。

3 个答案:

答案 0 :(得分:23)

我猜你需要一个只有可见值的列表。我说“值”因为属性不是实际值,它们是描述符。以下是Mage_Mage_Catalog_Block_Product_View_Attributes的重要部分:

$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
    if ($attribute->getIsVisibleOnFront()) {
        $value = $attribute->getFrontend()->getValue($product);
        // do something with $value here
    }
}

您实际上并不需要复制此内容,因为您可以更改/使用已在产品视图页面上声明为catalog/product/view/attributes.phtml块的模板attributes

答案 1 :(得分:14)

根据您的问题,您应该使用Mage::getResourceModel('catalog/product_attribute_collection')代替:

$productAttrs = Mage::getResourceModel('catalog/product_attribute_collection');

foreach ($productAttrs as $productAttr) { /** @var Mage_Catalog_Model_Resource_Eav_Attribute $productAttr */
    var_dump($productAttr->getAttributeCode());
}

您并不总是在_datagetData())存储中拥有属性,并且您并不总是需要加载产品才能获取其属性。

答案 2 :(得分:7)

它非常简单,并为您提供了一系列可用的产品属性名称

$product = Mage::getModel('catalog/product')->load('product_id');
$attributeNames = array_keys($product->getData());
print_r($attributeNames);

如果您需要属性对象集合,可以调用

$product->getAttributes();

如果您需要产品系列,之后您可以对每个收集成员执行前面提到的方法

Mage::getModel('catalog/product')->getCollection();