13

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 solutions that use

$attributes = $product->getAttributes();

but I cannot get this to work, it just brings up a blank page. Any help would be greatly appreciated, I have spent hours and hours on this so far...

I am using Magento version 1.4.2.0

UPDATE: Here is exactly what I am trying to do:

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

this is in the file gallery.phtml in design/adminhtml/default/default/catalog/product/helper/

For some reason, I cannot get the getAttributeCode function to return anything.

Charles
  • 50,943
  • 13
  • 104
  • 142
Chad
  • 271
  • 2
  • 7
  • 16

3 Answers3

23

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.

clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
  • this is great and works on the frontend, but I am trying to use this in the file gallery.phtml in design/adminhtml/default/default/catalog/product/helper/ - any ideas? It seems to not be able to get the product variable in the same way – Chad Feb 16 '11 at 14:56
  • I posted exactly what I am trying to do in my original post. Hope you can help! – Chad Feb 16 '11 at 15:19
  • Since you don't actually need a product's values does it work if you create a blank product with `Mage::getModel('catalog/product')`? This might not respect different attribute sets, I'm not sure how to find that out. – clockworkgeek Feb 16 '11 at 17:12
14

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.

nevvermind
  • 3,302
  • 1
  • 36
  • 45
7

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();
Anton S
  • 12,750
  • 2
  • 35
  • 37
  • Might also note on this that you need to set "Used in Product Listing" > Yes in the Attribute settings – Nathan Fitzgerald - Fitzgenius Jan 14 '15 at 11:55
  • I am getting this array [0] => Array ( [entity_id] => 12 [entity_type_id] => 4 [attribute_set_id] => 4 [type_id] => simple [sku] => 20707 [has_options] => 1 [required_options] => 1 [created_at] => 2016-01-06 21:15:31 [updated_at] => 2016-10-03 00:49:21 ) How can I get additional attributes like name , product image , description etc – Sachin Vairagi Dec 07 '16 at 08:31