I cannot figure this out!
I am trying to get a list of a products attributes into an array on the list.phtml page. I have tried everything. I have seen a lot of solution
I'm guessing you need a list of only visible values. I say "values" because attributes are not the actual values, they are descriptors. The following is the salient parts from 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
}
}
You don't really need to duplicate this though since you can alter/use the template catalog/product/view/attributes.phtml
which is already declared on the product view page as attributes
block.
It's rather easy and gives you an array of available product attribute names
$product = Mage::getModel('catalog/product')->load('product_id');
$attributeNames = array_keys($product->getData());
print_r($attributeNames);
If you need a attribute object collection you can call
$product->getAttributes();
If you need a product collection and after that you can perform the previously mentioned ways on each collection member
Mage::getModel('catalog/product')->getCollection();
According to your question, you should be using Mage::getResourceModel('catalog/product_attribute_collection')
instead:
$productAttrs = Mage::getResourceModel('catalog/product_attribute_collection');
foreach ($productAttrs as $productAttr) { /** @var Mage_Catalog_Model_Resource_Eav_Attribute $productAttr */
var_dump($productAttr->getAttributeCode());
}
You don't always have attributes in the _data
(getData()
) storage and you don't always need to load a product to get its attributes.