2011-02-10 118 views
13

我无法弄清楚这一点!得到一个magento中的所有产品属性的数组

我想获取产品属性列表到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; 
    } 
} 

这是在设计文件gallery.phtml/adminhtml /默认/缺省/目录/产品/帮手/

出于某种原因,我不能让getAttributeCode函数返回任何东西。

+0

您是否试过先获取产品收藏? – kjy112 2011-02-10 19:21:15

+1

你有没有像XDebug一样使用调试器进行逐步调试? – greg0ire 2011-02-10 19:54:33

+1

您是在询问特定产品的属性值,还是所有可能的属性代码列表? – 2011-02-10 22:36:03

回答

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 
    } 
} 

你并不真的需要复制这虽然因为你可以改变/使用已经宣告产品视图页面上attributes块模板catalog/product/view/attributes.phtml

6

这是相当容易的,给你提供的产品阵列属性名

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

如果你需要一个属性对象集合,你可以调用

$product->getAttributes(); 

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

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

可能还要注意这一点,你需要设置“产品清单中使用”>是属性设置 – 2015-01-14 11:55:07

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())存储属性,你并不总是需要加载一个产品获得其属性。

相关问题