Adding Custom Customer Attributes

If you you ever have a need to extend the customer profile within magento and add additional attributes to it, here is a good explanation on how to accomplish that.

In the example below, we will add hidden attributes which will later be used for importing clients and passwords from another system into Magento.

To do this, we need to create a separate module and we will use the MCorner namespace for it.

  1. Create NAMESPACE folder under app/code/community/MCorner
  2. Create module folder “CustomerAddCustomAttr” as we will use this one as the name of our module
    • app/code/community/MCorner/CustomerAddCustomAttr
  3. Create Folders
    • app/code/community/MCorner/CustomerAddCustomAttr/etc
    • app/code/community/MCorner/CustomerAddCustomAttr/Model/Resource/Eav/Mysql4
    • app/code/community/MCorner/CustomerAddCustomAttr/sql/CustomerAddCustomAttr_setup
  4. Create the config.xml file
    • app/code/community/MCorner/CustomerAddCustomAttr/etc/config.xml
  5. Inside the config.xml file copy and paste the code below:
    <?xml version="1.0"?>
    <config>
        <modules>
            <MCorner_CustomerAddCustomAttr>
                <version>0.1.1</version>
            </MCorner_CustomerAddCustomAttr>
        </modules>
        <global>        
            <resources>
                <CustomerAddCustomAttr_setup>
                    <setup>
                        <module>MCorner_CustomerAddCustomAttr</module>
                        <class>MCorner_CustomerAddCustomAttr_Model_Resource_Eav_Mysql4_Setup</class>
                    </setup>
                    <connection>
                        <use>core_setup</use>
                    </connection>
                </CustomerAddCustomAttr_setup>
                <CustomerAddCustomAttr_write>
                    <connection>
                        <use>core_write</use>
                    </connection>
                </CustomerAddCustomAttr_write>
                <CustomerAddCustomAttr_read>
                    <connection>
                        <use>core_read</use>
                    </connection>
                </CustomerAddCustomAttr_read>
            </resources>        
        </global>
    </config>
  6. Create the Local EAV Setup file:
    • app/code/community/MCorner/CustomerAddCustomAttr/Model/Resource/Eav/Mysql4/Setup.php
  7. Copy and Paste the code below into app/code/community/MCorner/CustomerAddCustomAttr/Model/Resource/Eav/Mysql4/Setup.php
    <?php
    class MCorner_CustomerAddCustomAttr_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup {    
    
    }
  8. Create the file for the attributes insertion
    • app/code/community/MCorner/CustomerAddCustomAttr/sql/CustomerAddCustomAttr_setup/mysql4-install-0.1.0.php
  9. Now comes the fun part, inside the app/code/community/MCorner/CustomerAddCustomAttr/sql/CustomerAddCustomAttr_setup/mysql4-install-0.1.0.php file copy and paste the code below:
    <?php
    $installer = $this;
    $installer->startSetup();
    
    $setup = Mage::getModel('customer/entity_setup', 'core_setup');
    
    $setup->addAttribute('customer', 'mcorner_password', array(
    	'type' => 'varchar',
    	'input' => 'text',
    	'label' => 'MCorner Password',
    	'global' => 1,
    	'visible' => 1,
    	'required' => 0,
    	'user_defined' => 0,
    	'default' => '',
    	'visible_on_front' => 0,
        'source' =>	 NULL,
    ));
    
    $setup->addAttribute('customer', 'mcorner_salt', array(
    	'type' => 'varchar',
    	'input' => 'text',
    	'label' => 'MCorner Salt',
    	'global' => 1,
    	'visible' => 1,
    	'required' => 0,
    	'user_defined' => 0,
    	'default' => '',
    	'visible_on_front' => 0,
        'source' =>	 NULL,
    ));
    
    $setup->addAttribute('customer', 'mcorner_pass_patched', array(
    	'type' => 'varchar',
    	'input' => 'text',
    	'label' => 'MCorner Flag For Pass Patched',
    	'global' => 1,
    	'visible' => 1,
    	'required' => 0,
    	'user_defined' => 0,
    	'default' => 'n',
    	'visible_on_front' => 0,
        'source' =>	 NULL,
    ));
    
    $installer->endSetup();
  10. Finally, to activate the CustomerAddCustomAttr module create the module file:
    • app/etc/modules/MCorner_CustomerAddCustomAttr.xml
  11. Copy and paste the code below
    <config>
        <modules>
            <MCorner_CustomerAddCustomAttr >
                <active>true</active>
                <codePool>community</codePool>
            </MCorner_CustomerAddCustomAttr >
        </modules>
    </config>
  12. Make sure all files are saved in their locations as in the tutorial and now go into Magento admin and CLEAR ALL CACHE.

If you followed the steps above, you will now get the customer profile extended with new attributes as specified in the installation file.

The same technique can be used to extend the customer profile with additional fields which can be set to be visible on front:

('visible_on_front' => 1)

Additionally, to attach the attributes to actual forms you can use the code below:

Mage::getSingleton('eav/config')
	->getAttribute('customer', 'your_front_visible_attribute_code')
	->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'))
	->save();

if (version_compare(Mage::getVersion(), '1.6.0', '<='))
{
	$customer = Mage::getModel('customer/customer');
	$attrSetId = $customer->getResource()->getEntityType()->getDefaultAttributeSetId();
	$setup->addAttributeToSet('customer', $attrSetId, 'General', 'your_front_visible_attribute_code');
}

if (version_compare(Mage::getVersion(), '1.4.2', '>='))
{
	Mage::getSingleton('eav/config')
	->getAttribute('customer', 'your_front_visible_attribute_code')
	->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'))
	->save();

}

11 thoughts on “Adding Custom Customer Attributes

  1. hi guy!!

    I just followed step by step and I got an exception 🙁
    The only difference is that I placed my files in local instead community directory…
    I work with magento 1.7

    Any idea?

    Regrads

  2. Hi Jonay.,

    I think you left the file “app/etc/modules/MCorner_CustomerAddCustomAttr.xml” as it was?

    <config>
    <modules>
    <MCorner_CustomerAddCustomAttr >
    <active>true</active>
    <codePool>community</codePool>
    </MCorner_CustomerAddCustomAttr >
    </modules>
    </config>

    The tag “community” should match your files location so if you have left that as community and you placed the files under /app/etc/code/local, change the code to

    <config>
    <modules>
    <MCorner_CustomerAddCustomAttr >
    <active>true</active>
    <codePool>local</codePool>
    </MCorner_CustomerAddCustomAttr >
    </modules>
    </config>

    and then clear cache!!! To make Magento reload the config xml files.

    Additionally, check your exception log: /var/logs/exception.log and system.log, They have info as of what was the exact error.

    Let us know how it goes.

  3. Pingback: Adding Custom Customer Attributes

  4. Hi Stevo , all worked well and i can save it on customer edit area , atrribute code is payment_term

    i would like ask , how can i write the attribute on the invoice ? i tried a few things but it came with empty result

    thanks

  5. Hi Ibrahim,

    The customer is an EAV model meaning you can add attributes to it on the fly.

    When it comes to the Invoice that is a regular model and you would need to add an sql update and add this additional field to the sales_flat_invoice table.

    I hope the info helps.

  6. Hi,

    Followed your guide step by step and received the following error.

    Fatal error: Class ‘MCorner_CustomerAddCustomAttr_Model_Resource_Eav_Mysql4_Setup’ not found in app/code/core/Mage/Core/Model/Resource/Setup.php on line 234

    Any ideas?

    Version 1.7.0.2

    Thanks.

Leave a Reply

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

*