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
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
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.