Magento get attribute type (e.g. dropdown or text)

Ste*_*dle 3 php magento

I'd like to know how to get the type of a product attribute object. In the magento backend it is required to select between various options like "text field" or "drop down".

I'm working with a product import script and it's important to know, which type of attribute it is to set the value correctly.

Ste*_*dle 11

There's a simple magic method to get the value of the object:

$attribute = Mage::getModel('eav/entity_attribute')->load( $your_attribute_id );
$attribute->getFrontendInput();
Run Code Online (Sandbox Code Playgroud)

The result is a short string, for example "text" or "select". Here's a short list of all types in Magento 1.7 (german translation):

  • text: Einzeiliges Textfeld
  • textarea: Mehrzeiliger Textbereich
  • date: Datum
  • boolean: Ja/Nein
  • multiselect: Mehrfach Auswahl
  • select: selected="selected: Drop-Down
  • price: Preis
  • media_image: Bild
  • weee: Feste Produktsteuer (FPT)

If you need a list of all options from a single attribute, do this:

Mage::getModel( 'eav/config' )->getAttribute( 'catalog_product' , 'code_of_attribute' )
Run Code Online (Sandbox Code Playgroud)

So you've load the attribute object. Other methods for loading an object doesn't work for me (e.g. Mage::getModel('eav/entity_attribute')->load('xy'); ).

Then use the getSource() method and the getAllOptions method to recieve an array with all options:

$your_attribute->getSource()->getAllOptions(true, true)
Run Code Online (Sandbox Code Playgroud)

Result looks like this:

array(4) {
  [0]=>
  array(2) {
    ["label"]=>
    string(0) ""
    ["value"]=>
    string(0) ""
  }
  [1]=>
  array(2) {
    ["value"]=>
    string(1) "5"
    ["label"]=>
    string(6) "red"
  }
  [2]=>
  array(2) {
    ["value"]=>
    string(1) "4"
    ["label"]=>
    string(6) "blue"
  }
  [3]=>
  array(2) {
    ["value"]=>
    string(1) "3"
    ["label"]=>
    string(6) "green"
  }
}
Run Code Online (Sandbox Code Playgroud)