Adding new attribute to Customer Account in Magento
by Dan on Jul.19, 2011, under Magento, PHP
I recently discovered that this is a right pig to get going. There’s plenty of “solutions” on the interwebs. Many seemingly either don’t work, don’t work as expected, or are complete hacks which makes your extension not modular (i.e; by executing the “addAttribute” function within a template, then removing it once it’s done).
Here’s how I did it. Testing working in Magento 1.5.1.0. Create the following directory structure;
app/code/local/BTS app/code/local/BTS/Customer app/code/local/BTS/Customer/etc app/code/local/BTS/Customer/Model app/code/local/BTS/Customer/Model/Entity app/code/local/BTS/Customer/sql app/code/local/BTS/Customer/sql/bts_customer_setup
Create the file app/code/local/BTS/Customer/etc/config.xml;
<?xml version="1.0"?> <config> <modules> <BTS_Customer> <version>0.0.1</version> </BTS_Customer> </modules> <global> <fieldsets> <customer_account> <samples_ordered><create>1</create><update>1</update></samples_ordered> </customer_account> </fieldsets> <models> <bts_customer> <class>BTS_Customer_Model</class> </bts_customer> </models> <resources> <bts_customer_setup> <setup> <module>BTS_Customer</module> <class>BTS_Customer_Model_Entity_Setup</class> </setup> </bts_customer_setup> </resources> </global> </config>
Create the file app/code/local/BTS/Customer/Model/Entity/Setup.php;
<?php class BTS_Customer_Model_Entity_Setup extends Mage_Customer_Model_Entity_Setup {}
Create the file app/code/local/BTS/Customer/sql/bts_customer_setup/mysql4-install-0.0.1.php;
<?php $installer = $this; $installer->startSetup(); $installer->addAttribute('customer', 'samples_ordered', array( 'label' => 'Number of samples ordered', 'type' => 'int', 'input' => 'text', 'visible' => true, 'required' => false, 'position' => 9999, )); $installer->endSetup();
And finally, create the file app/etc/modules/BTS_Customer.xml;
<?xml version="1.0"?> <config> <modules> <BTS_Customer> <active>true</active> <codePool>local</codePool> </BTS_Customer> </modules> </config>
I stopped at this point, as all I needed the new attribute for was for system counting internally. This won’t display the attribute on the admin panel though.
December 14th, 2011 on 9:48 pm
Hi! Very useful things. It really helped me! I followed the steps you suggested but I have an error for a select field. I added for register form some fields with details. I also used a select and I can’t figure out why it doesn’t work. it doesn’t save my selected value when I register, but then from edit account if i select a value it remains selected. Also from admin, when i create an account, it saves the values for my select field. It doesn’t work only for my register form from frontend. Do you have any idea?
Thanks so much,
Denisa
April 30th, 2012 on 9:27 pm
good, i didnt get any error and installed it, but how to show under admin panel ?
May 1st, 2012 on 7:48 am
@quizz;
The code in this article only adds an attribute for programmatic use (within custom extensions, etc). To get this to show up in the admin area, you’ll need to start cloning and modifying the adminhtml template files, as I believe this is fixed and not as dynamic as product attributes are. Categories are the same.
I’ve not actually had to do anything in terms of putting a custom attribute against a customer and have it displayed in the admin area, so it’s not that I don’t want to help you, it’s that I don’t want to give you incorrect information. However, if I ever do come across a scenario that requires this, I’ll be certain to update this post with the relevant information.
If, in the meantime, you find out how to do this correctly, please let me know and I’ll post it here with credit to you.
Dan
May 3rd, 2012 on 11:39 am
Hi, I need the same functionality to upload file in backend of new customer ? Any help for me ?