Magento 1.9 – Create Custom Dropdown Attribute for Customer

dropdown-attributemagento-1.9

I want to create a custom dropdown attribute for magento customer account.

For this I have used $installer script but it is not working for me. My script as below :

<?php
$installer = $this;
$installer->startSetup();

$installer->addAttribute("customer", "track_device",  array(
    "label"    => "Customer Device",
    'input' => 'select',
    'type'  => 'int',
    'required' => 0,
    'user_defined' => 0,
    'source' => 'eav/entity_attribute_source_table',
    'option' => array('values' => array('Web', 'App')) 

));

$attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "track_device");

$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100)
        ;
$attribute->save();

$installer->endSetup();
?>

Is anything missing or wrong in this script?

Is any another method to create custom dropdown attribute in customer account in magento?

Best Answer

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();


?>