Adding new attribute to Customer Account in Magento

Advertisements

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;



    
        
            0.0.1
        
    
    
        
            
                11
            
        
        
            
                BTS_Customer_Model
            
        
        
            
                
                    BTS_Customer
                    BTS_Customer_Model_Entity_Setup
                
            
        
    

Create the file app/code/local/BTS/Customer/Model/Entity/Setup.php;


Create the file app/code/local/BTS/Customer/sql/bts_customer_setup/mysql4-install-0.0.1.php;

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;



    
        
            true
            local
        
    

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.

Join the Conversation

4 Comments

  1. 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

  2. @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

  3. Hi, I need the same functionality to upload file in backend of new customer ? Any help for me ?

Leave a comment

Your email address will not be published. Required fields are marked *