Get a list of all product attributes in magento

I have been doing frontend magento for a while, but just started creating modules. This is what I know how to do the interface, but I'm afraid in my module. What I'm trying to achieve at the moment fills multiselect in admin with all available product attributes. Including custom product attributes for all product attribute sets. I'm not quite sure which table this will require, because I don't want to assume that Flat data is included.

I created my administration area on a new tab in the system configuration, I created a multi selector field, which is currently populated with three static parameters. It works a lot. Can someone help me by pointing my finger in the right direction ... Currently, this is what I still have (for what it's worth).

<?php class test_test_Model_Source { public function toOptionArray() { return array( array('value' => 0, 'label' =>'First item'), array('value' => 1, 'label' => 'Second item'), array('value' => 2, 'label' =>'third item'), ); } } 

///////////////////////////// EDIT //////////////////// /////////////////

I feel like I can be on something here, but it only returns the first letter of each attribute (so I'm not sure if even its attributes are returned)

 public function toOptionArray() { $attributes = Mage::getModel('catalog/product')->getAttributes(); $attributeArray = array(); foreach($attributes as $a){ foreach($a->getSource()->getAllOptions(false) as $option){ $attributeArray[$option['value']] = $option['label']; } } return $attributeArray; } 

///////////////////////////////// EDIT //////////////// //////////////////////

I'm not very close, because now I know that the array returns what I want, all the code_attributes. However, he still only displays the first letter of each ... Does anyone know why?

 public function toOptionArray() { $attributes = Mage::getModel('catalog/product')->getAttributes(); $attributeArray = array(); foreach($attributes as $a){ foreach ($a->getEntityType()->getAttributeCodes() as $attributeName) { $attributeArray[$attributeName] = $attributeName; } break; } return $attributeArray; } 
+7
source share
3 answers

I answered my question. I found a way that worked, but I'm not sure why, therefore, if someone can comment and explain what would be helpful. Therefore, having the attribute $ attributeArray [$ attributeName] = $ attributeName; worked when it came to print_r, when you returned the array, it provided only the first letter. However, if you do the following, which, in my opinion, seems to do the same thing as it works. I can only imagine that when rendering it was not expecting a string, but something else. Anyway, here is the code:

 public function toOptionArray() { $attributes = Mage::getModel('catalog/product')->getAttributes(); $attributeArray = array(); foreach($attributes as $a){ foreach ($a->getEntityType()->getAttributeCodes() as $attributeName) { //$attributeArray[$attributeName] = $attributeName; $attributeArray[] = array( 'label' => $attributeName, 'value' => $attributeName ); } break; } return $attributeArray; } 
+6
source

No need to do extra cycles, as Frank Clark suggested. Just use:

 public function toOptionArray() { $attributes = Mage::getResourceModel('catalog/product_attribute_collection')->addVisibleFilter(); $attributeArray = array(); foreach($attributes as $attribute){ $attributeArray[] = array( 'label' => $attribute->getData('frontend_label'), 'value' => $attribute->getData('attribute_code') ); } return $attributeArray; } 
+3
source

You can try to get attributes in a different way, for example

 $attributes = Mage::getSingleton('eav/config') ->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection(); 

Once you have the attributes, you can get the options this way (copied from magenta code)

 $result = array(); foreach($attributes as $attribute){ foreach ($attribute->getProductAttribute()->getSource()->getAllOptions() as $option) { if($option['value']!='') { $result[$option['value']] = $option['label']; } } 

}

+1
source

All Articles