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

Cha*_*had 13 php zend-framework magento magento-1.4

我想不出来了!

我试图在list.phtml页面上将产品属性列表放入一个数组中.我尝试了一切.我见过很多使用的解决方案

$attributes = $product->getAttributes();
Run Code Online (Sandbox Code Playgroud)

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

我使用的是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;
   }
 }
Run Code Online (Sandbox Code Playgroud)

这是在design/adminhtml/default/default/catalog/product/helper /中的gallery.phtml文件中

出于某种原因,我无法获取getAttributeCode函数来返回任何内容.

clo*_*eek 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
    }
}
Run Code Online (Sandbox Code Playgroud)

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


nev*_*ind 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());
}
Run Code Online (Sandbox Code Playgroud)

您并不总是在_data(getData())存储中具有属性,并且您并不总是需要加载产品来获取其属性.


Ant*_*n S 7

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

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

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

$product->getAttributes();
Run Code Online (Sandbox Code Playgroud)

如果您需要产品系列,那么您可以在每个收集成员上执行前面提到的方法

Mage::getModel('catalog/product')->getCollection();
Run Code Online (Sandbox Code Playgroud)