2

I want to introduce a new field in customer admin area and I just want to have it two options "yes" or "no"

here is my code that is creating simple text fields but I am not sure how can I create drop down attribute for the customer?

$installer->startSetup();
$vCustomerEntityType = $installer->getEntityTypeId('customer');
$vCustAttributeSetId = $installer->getDefaultAttributeSetId($vCustomerEntityType);
$vCustAttributeGroupId = $installer->getDefaultAttributeGroupId($vCustomerEntityType,$vCustAttributeSetId);
$installer->addAttribute('customer', 'testveri', array(
    'label' => 'Working?',
    'input' => 'text',
    'type'  => 'varchar',
    'forms' => array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'),
    'required' => 0,
    'user_defined' => 0,
));
$installer->addAttributeToGroup($vCustomerEntityType, $vCustAttributeSetId,     $vCustAttributeGroupId, 'testveri', 0);
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'testveri');
$oAttribute->setData('used_in_forms', array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'));
$oAttribute->save();
$installer->endSetup();

Can anybody help me in creating a drop down attribute for admin panel only?

4 Answers 4

4

Try this:

$installer->addAttribute('customer', 'testveri', array(
    'label' => 'Working?',
    'input' => 'select',
    'type'  => 'int',
    'required' => 0,
    'user_defined' => 0,
    'source' => 'eav/entity_attribute_source_table',
    'option' => array('values' => array('Value 1', 'Value 2', 'Value 3')) 
));

EDIT
Add the new attribute as a column in the grid.
Override the default customer grid and in _prepareColumns add this:

protected function _prepareColumns(){ 
    $values = array(
        1   => Mage::helper('customer')->__('Yes'),
        0   => Mage::helper('customer')->__('No') 
    );
    $this->addColumnAfter('testveri', array( 
        'header' => Mage::helper('customer')->__('Working?'), 
        'index' => 'testveri', 
        'type'      =>  'options',
        'options'   =>  $values, 
    ),'email'); 
    return parent::_prepareColumns(); 
}
8
  • Its having one issue. its not showing values as Yes or no instead its showing the value IDs i.e. for yes its showing 5 and for "no" its showing 6
    – Kuldeep
    Commented Jul 24, 2013 at 8:59
  • 1
    If you want a yes/no attribute change the source in the code above to eav/entity_attribute_source_boolean and remove option.
    – Marius
    Commented Jul 24, 2013 at 9:12
  • no it doesn't work still now it have started showing 1 or 0 but still not Yes or no :( please help
    – Kuldeep
    Commented Jul 24, 2013 at 9:17
  • Where do you get 1 and 0? in the form or when displaying it somewhere? Can you post the full code of your module and indicate where the problem occurs?
    – Marius
    Commented Jul 24, 2013 at 9:28
  • I am getting 1 and 0 on the customer grid. I am adding the column on the customer grid. protected function _prepareColumns(){ $this->addColumnAfter('testveri', array( 'header' => Mage::helper('customer')->__('Working?'), 'index' => 'testveri', ),'email'); return parent::_prepareColumns(); }
    – Kuldeep
    Commented Jul 24, 2013 at 9:37
0

You can try below code to add drop-down value attribute for customer

<?php

$installer = $this;

/* @var $installer Mage_Customer_Model_Entity_Setup */
$installer->startSetup();

$installer->addAttribute('customer', 'testveri', array(
    'label'        => 'Working?',
    'visible'      => true,
    'required'     => false,
    'type'         => 'int',
    'input'        => 'select',
    'source'        => 'eav/entity_attribute_source_table',
));


$tableOptions        = $installer->getTable('eav_attribute_option');
$tableOptionValues   = $installer->getTable('eav_attribute_option_value');

// add options for level of politeness
$attributeId = (int)$installer->getAttribute('customer', 'testveri', 'attribute_id');
foreach (array('Yes', 'No') as $sortOrder => $label) {

    // add option
    $data = array(
        'attribute_id' => $attributeId,
        'sort_order'   => $sortOrder,
    );
    $installer->getConnection()->insert($tableOptions, $data);

    // add option label
    $optionId = (int)$installer->getConnection()->lastInsertId($tableOptions, 'option_id');
    $data = array(
        'option_id' => $optionId,
        'store_id'  => 0,
        'value'     => $label,
    );
    $installer->getConnection()->insert($tableOptionValues, $data);

}

$genderAttribute = Mage::getSingleton('eav/config')->getAttribute('customer_address', 'testveri');
$genderAttribute->setData('used_in_forms', array(
     'adminhtml_customer',
     'customer_address_edit',
     'customer_register_address'
));
$genderAttribute->save();

$installer->endSetup();
?>
0

try

$installer->addAttribute('customer', 'testveri', array(
    'label' => 'Working?',
    'input' => 'select',
    'type'  => 'int',
    'source'  => 'eav/entity_attribute_source_boolean',
    'required' => 0,
    'user_defined' => 0,
));

$attributeCode='testveri';
$attribute = Mage::getSingleton('eav/config')
    ->getAttribute('customer', $attributeCode);
$attribute->setData('used_in_forms', array('adminhtml_customer'));
$attribute->save(); 
0

Hello Friend i have found some small and Quick solution for creating attribute for customer.

Login your Admin and follow the stapes.

Catalog -> Attribute -> Manage Attributes

Now create an attribute. It might be text box or drop down

Now open your phpmyadmin & find out eav_attribute table Now find out your attribute with your attribute_code and change entity_type_id with 1. Because 1 stand for customer attribute.

Now from any controller just run the following code

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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