How to Add Custom Customer Attributes in Magento eCommerce Development?
Do you need to extend the customer profile within Magento and add additional attributes to it? Are you looking for a simple way to add new fields to customer account pages? Let’s learn how to add custom customer attributes to your Magento website.
In the given example, we would show how to add hidden attributes that can be used for importing clients and passwords from another system into Magento:
1. Create NAMESPACE folder under app/code/local/Custom
2. Create module folder Attribute as we will use this one as the name of our module
app/code/local/Custom/Attribute
3. Create Folders as:
app/code/local/Custom/Attribute/etc
app/code/local/Custom/Attribute/Model/Resource/Eav/Mysql4
app/code/local/Custom/Attribute/sql/Attribute_setup
4. Create the config.xml file
app/code/local/Custom/Attribute/etc/config.xml
5. Inside the config.xml file, copy and paste the code below:
<?xml version="1.0"?>
<config>
<modules>
<Custom_Attribute>
<version>0.1.1</version>
</Custom_Attribute>
</modules>
<global>
<resources>
<Attribute_setup>
<setup>
<module>Custom_Attribute</module>
<class>Custom_Attribute_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</Attribute_setup>
<Attribute_write>
<connection>
<use>core_write</use>
</connection>
</Attribute_write>
<Attribute_read>
<connection>
<use>core_read</use>
</connection>
</Attribute_read>
</resources>
</global>
</config>
6. Create the Local EAV Setup file:
app/code/local/Custom/Attribute/Model/Resource/Eav/Mysql4/Setup.php
7. Copy and Paste the code below into app/code/local/Custom/Attribute/Model/Resource/Eav/Mysql4/Setup.php
<?php
class Custom_Attribute_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup {
}
8. Create the file for the attributes insertion as:
app/code/local/Custom/Attribute/sql/Attribute_setup/mysql4-install-0.1.0.php
9. Now, here comes the fun part, inside the app/code/local/Custom/Attribute/sql/Attribute_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', 'custom_attribute1', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Custom Attribute 1',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'default' => '',
'visible_on_front' => 0,
'source' => NULL,
));
$setup->addAttribute('customer', 'custom_attribute2', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Custom Attribute 2',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'default' => '',
'visible_on_front' => 0,
'source' => NULL,
));
$setup->addAttribute('customer', 'custom_attribute3', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Custom Attribute 3',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'default' => 'n',
'visible_on_front' => 0,
'source' => NULL,
));
$installer->endSetup();
10. In order to activate the Attribute module, create the module file:
app/etc/modules/Custom_Attribute .xml
11. Copy and paste this code
<config>
<modules>
< Custom_Attribute>
<active>true</active>
<codePool>local</codePool>
</Custom_Attribute>
</modules>
</config>
12. Ensure all files are saved in their locations as in the tutorial and now go to Magento admin and CLEAR ALL CACHE.
Once you have followed the steps above, you will be able to get the customer profile extended with new attributes as specified in the installation file.
You can use the same technique to extend the customer profile with additional fields that can be set to be visible on the front:
('visible_on_front' => 1)
If you want 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();
}
Give it a try and add more custom customer attributes to your Magento store. Good luck.