27

How can I fetch a list (array) of all defined product attributes? (basically I need the code and label).

edit:

The attributes I need this to dynamically generate ACLs for all product attributes that exist in the shop. (working on a module for Hide Attributes and Other Fields in Product Edit Backend)

2
  • Ignoring attribute sets?
    – benmarks
    Commented May 10, 2013 at 14:53
  • @benmarks: Yes.
    – Alex
    Commented May 10, 2013 at 15:01

3 Answers 3

41

If you need a MySQL Query, try that:

select attribute_id, attribute_code, frontend_label from eav_attribute where entity_type_id IN (select entity_type_id from eav_entity_type where entity_type_code = 'catalog_product')

An alternative to Fabian code, if you need a PHP script based on Magento, try this:

$attributes = Mage::getResourceModel('catalog/product_attribute_collection')
    ->getItems();

foreach ($attributes as $attribute){
    echo $attribute->getAttributecode();
    echo '<br>';
    echo $attribute->getFrontendLabel();
}
3
  • Nice. Mage_Catalog_Model_Resource_Product_Attribute_Collection does basically what was Fabian trying to craft. Thanks!
    – Alex
    Commented May 10, 2013 at 15:00
  • you're welcome :-) Commented May 10, 2013 at 15:13
  • how do i get a list of attributes using a group id ? not attribute sets Commented Jul 28, 2014 at 12:29
5

//Mage_Eav_Model_Mysql4_Entity_Attribute_Collection
Mage::getResourceModel('eav/entity_attribute_collection')->setEntityTypeFilter(Mage_Catalog_Model_Product::ENTITY);

should do.

We just found a bug, you have to pass the entity_type_id:

$col = Mage::getResourceModel('eav/entity_attribute_collection')->setEntityTypeFilter(4);

DOES

The code is the documentation:

if ($type instanceof Mage_Eav_Model_Entity_Type) {
        $additionalTable = $type->getAdditionalAttributeTable();
        $id = $type->getId();
    } else {
        $additionalTable = $this->getResource()->getAdditionalAttributeTable($type);
        $id = $type;
    }

hopefully working solution (updated by @Alex comment)

You have to pass a Mage_Eav_Model_Entity_Type so this should work and is not hardcoded:

$type = Mage::getModel('eav/entity_type')->loadByCode(Mage_Catalog_Model_Product::ENTITY)
Mage::getResourceModel('eav/entity_attribute_collection')->setEntityTypeFilter($type);
8
  • The collection returns a count of 0 ... I remember there was some kind of API or service class for this, but can't find it ATM.
    – Alex
    Commented May 10, 2013 at 14:24
  • Great - but 4 is hardcoded (even no sooo likely to change). Fix it for an upvote :-)
    – Alex
    Commented May 10, 2013 at 14:34
  • And no, the first line does not work. Mage_Catalog_Model_Product::ENTITY is a string, not an Mage_Eav_Model_Entity_Type
    – Alex
    Commented May 10, 2013 at 14:36
  • I think I got it now :D Commented May 10, 2013 at 14:44
  • But then I'm to late. I'll do, when I pass the 2k rep :p Commented May 10, 2013 at 14:48
3

This is to get all attributes

SELECT
    eav_attribute_option_value.option_id,
    eav_attribute_option_value.`value`,
    eav_attribute_option.attribute_id
                FROM
                        eav_attribute_option_value
                INNER JOIN eav_attribute_option ON eav_attribute_option_value.option_id = eav_attribute_option.option_id
                WHERE
                        eav_attribute_option.attribute_id = 135
                OR eav_attribute_option.attribute_id = 142 
                -- 135 = BRANDS
                -- 142 = TYPES
                GROUP BY
                        eav_attribute_option_value.option_id
                ORDER BY
                eav_attribute_option_value.`value` ASC
2
  • Don't want to use plain SQL ...
    – Alex
    Commented Jul 30, 2016 at 8:22
  • thats a good one, thanks
    – dsomnus
    Commented Feb 23 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.