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):
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)
| 归档时间: |
|
| 查看次数: |
11332 次 |
| 最近记录: |