I want to create Dropdown Attribute for Products and want to fetch data from the database in created Dropdown Attribute.
Magento 1.9 – Create Dropdown Attribute for Products
databasemagento-1.9product-attribute
Related Solutions
Try this:
<?php
require_once('app/Mage.php');
Mage::app('default');
$installer = new Mage_Customer_Model_Entity_Setup('core_setup');
$installer->startSetup();
$entityTypeId = (int)$installer->getEntityTypeId('customer');
$attributeSetId = (int)$installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = (int)$installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttribute('customer', 'track_device', array(
'type' => 'int',
'label' => 'Customer Device',
'input' => 'select',
'forms' => array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'),
'source' => 'eav/entity_attribute_source_table',
'required' => false,
'visible' => 1,
'user_defined' => true, /* To display in frontend */
'position' => 110,
'option' => array('values' => array('Web', 'App'))
));
$installer->addAttributeToGroup($entityTypeId, $attributeSetId, $attributeGroupId, 'track_device', 100);
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'track_device');
$oAttribute->setData('used_in_forms', array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'));
$oAttribute->save();
$installer->endSetup();
?>
You did not create Customer attribute properly.
As you have create a dropdown Customer attribute so you need to define source model for this attribute.
"source" => "[module_Model_Prefix]/source_option",
basically at this source class you have define option for that attribute
Also need to remove 'option' => array('values' => array('Inactive', 'Candidate', 'Certified')),
Source class
<?php
class [ModuleNameSpace]_[ModuleName]_Model_Source_Option extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
/**
* Retrieve all options array
*
* @return array
*/
public function getAllOptions()
{
if (is_null($this->_options)) {
$this->_options = array(
array(
"label" => Mage::helper("eav")->__("Inactive"),
"value" => 1
),
array(
"label" => Mage::helper("eav")->__("Candidate"),
"value" => 2
),
);
}
return $this->_options;
}
/**
* Retrieve option array
*
* @return array
*/
public function getOptionArray()
{
$_options = array();
foreach ($this->getAllOptions() as $option) {
$_options[$option["value"]] = $option["label"];
}
return $_options;
}
/**
* Get a text for option value
*
* @param string|integer $value
* @return string
*/
public function getOptionText($value)
{
$options = $this->getAllOptions();
foreach ($options as $option) {
if ($option["value"] == $value) {
return $option["label"];
}
}
return false;
}
Best Answer
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
Try above code and let me know if it is not working