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();

}