Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of all product attributes in magento

Tags:

model

magento

I have been doing frontend magento for a while but have only just started building modules. This is something i know how to do frontend but i am struggling with in my module. What i am trying to achieve for now, is populating a multiselect in the admin with all available product attributes. Including custom product attributes across all product attribute sets. I'm not entirely sure what table this will require because i don't want to assume that Flat Category Data is enabled.

I have created my admin area in a new tab in system config, i have created a multiselect field that is currently just being populated with three static options. This much works. Could anyone help me by pointing a finger in the right direction... currently this is what i have so far (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 might be onto something here, but it's only returning the first letter of every attribute (so i'm not sure if its even the attributes its returning)

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 am not extremely close as i now know that the array is returning what i want it to, all attribute_codes. However it is still only outputting the first letter of each... 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; 
}
like image 977
Frank Clark Avatar asked Apr 02 '13 18:04

Frank Clark


2 Answers

I have answered my own question. I have found a way that worked however i'm not sure why, so if someone could comment and explain that would be useful. So although having $attributeArray[$attributeName] = $attributeName; worked when it came to a print_r when you returned the array it was only providing the first letter. However if you do the following, which in my opinion seems to be doing exactly the same thing it works. I can only imagine that when rendering it wasn't 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; 
}
like image 143
Frank Clark Avatar answered Sep 19 '22 20:09

Frank Clark


No need to do additional loops, 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; 
}
like image 25
freento Avatar answered Sep 20 '22 20:09

freento