3

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?

6

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


?>
  • Hello @Abdul, this is not working for me, I have not found Customer Device dropdown field in customer account information tab in magento admin panel after apply this script. – Vinaya Maheshwari Feb 6 '16 at 5:20
  • Pls refresh magento cache to admin side system -> Cache Management and refresh all caches and check again in customer acoount – Abdul Feb 6 '16 at 5:25
  • I have already remove cache, reindex data, logout from admin panel and login again but it is not showing me any custom field in customer account. – Vinaya Maheshwari Feb 6 '16 at 5:28
  • 1
    Pls create test.php file at magento root folder and add bellow code in file – Abdul Feb 6 '16 at 5:29
  • updated ans so please add code in test.php file and run this file in browser after check in customer account – Abdul Feb 6 '16 at 5:31
1
require_once("app/Mage.php");
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
Mage::app('default');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');

//$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$setup->addAttribute('customer', 'note', array(
    'type'      => 'text',
    'label'     => 'Note',
    'global' => 1,
    'visible' => 1,
    'input'     => 'textarea',
    'position'  => 32,
    'required'  => 0,
    'user_defined' => 1,
));

$setup->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'note',
    '100'
);

$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'note');
$oAttribute->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit')); 
$oAttribute->save();

$setup->endSetup();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.