May 10, 2013
kalpesh

Magento add attribute to customer

In Magento to add an attribute to customer is not an option in the admin panel like it does have for Product attribute. So you have to end up writing the script that will add your custom attribute in customer’s EAV tables.

Below code will insert your custom customer attribute in Magento system. You can even specify whil creating the attribute whether that attribute should appear in the forms (like register/signup) or not.

$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', 'your_attribute_code_here', array(
    'input'         => 'text', //or select or whatever you like
    'type'          => 'int', //or varchar or anything you want it
    'label'         => 'Attribute description goes here',
    'visible'       => 1,
    'required'      => 0, //mandatory? then 1
    'user_defined' => 1,
));

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

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

$setup->endSetup();

Here we have used our custom attribute’s:
input type as text, but you can have it anything you like to appear in the form. It can be textarea, date, select or anything you want.
– data type as int, but you can have it anything from text, datetime, varchar or decimal. Remember customer is EAV in Magento, so it needs this information to store all the future values of this attribute in customer_entity_int (customer_entity_*) table.

5 Comments

  • Hi, I’m trying to add attribute to customer.

    it’s my first time with Magento.

    How i can run your script?

    Thank you very much for your help.

    Filippo

  • Hi Kalpesh,

    Thanks for sharing your code.

    It worked like a charm. However, for beginners, you may add the steps like activating the module, adding folders and all that.

    Your Fan,

    Anil

  • Great tut – i would also like to see the ‘beginner steps’ for this 🙂

  • Thanks for the tutorial. It is great and has helped me correct.
    But I have a problem. I want to assign the customer prices per category.
    For example, This customer gets for category 1 the price of the customer group B, for category 2 the price of the customer group C and for category 3 the price of the customer group A.
    That’s why I created three fields. But somehow does not work. Has anyone of you a solution or possibly an extension that has the function?
    Thanks in advance!

  • Mr. can you tell me path where should i type this code. and i want to create attribute under account information tab.

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions