I want to create Dropdown Attribute for Products and want to fetch data from the database in created Dropdown Attribute.
2 Answers
$attribute = Mage::getModel('eav/config')->getAttribute( Mage_Catalog_Model_Product::ENTITY ,'attribute_code');
$options = $attribute->getSource()->getAllOptions();
print_r ($options);
<select name="some name">
<?php foreach($options as $option): ?>
<option value="<?php echo $option['value'] ?>"><?php echo $option['label'] ?></option>
<?php endforeach;?>
</select>
My updated answer to you
load your table collection by
$coll = Mage::getModel('yourModel')->getCollection();
And foreach loop in your collection add all your option in $option as lable and value pair for each option
$attribute_model = Mage::getModel('eav/entity_attribute');
$attribute_code = $attribute_model->getIdByCode('catalog_product', 'your_attribute_code');
$attribute = $attribute_model->load($attribute_code);
$attribute->setData('option',$option);
$attribute->save();
Try above code and let me know if it is not working
-
-
It will give you all option and you can add options using foreach loop Commented Sep 1, 2016 at 11:41
-
it gives output like this Array ( [0] => Array ( [label] => [value] => ) ) Commented Sep 1, 2016 at 11:53
-
-
You need to create a source model (like like Mage_Tax_Model_Class_Source_Product) as well as an attribute that uses the source model.
In the source model you need to query the table and return the results in the getAllOptions function.
You can look here for a guide:
http://inchoo.net/magento/how-to-create-custom-attribute-source-type/