Create customer attribute in Magento

Hi Guys, it has been ages, hope you guys are still reading and enjoying our articles. Things have been really busy for us but at the same time really good for us as we have been working on a lot of client implementations but don’t worry we will still carry on writing useful articles for the community because we enjoy sharing.

Today we are going to talk about how to create a custom customer attribute in Magento. This requirement is very common as in community edition there is no way to create custom attribute for customer entity even though in enterprise you should be able to create your own customer attributes via admin. Anyways let’s crack on with creating a simple module to add a custom attribute to customer entity.

Step 1 – Create a new module under your local folder, in our case under applocalScommerceCustomer.

Step 2 – Register our module in etcmodules directory either in Scommerce_All.xml or Scommerce_Customer.xml by using the following code -:

<Scommerce_Customer> <active>true</active> <codePool>local</codePool>
</Scommerce_Customer>

Step 3 – Create config.xml under etc directory of your module applocalScommerceCustomeretcconfig.xml by using the following code

<?xml version="1.0" encoding="UTF-8"?>
<config> <modules> <Scommerce_Customer> <version>0.0.1</version> </Scommerce_Customer> </modules> <global> <models> <scommercecustomer> <class>Scommerce_Customer_Model</class> </scommercecustomer> </models> <resources> <scommercecustomer_setup> <setup> <module>Scommerce_Customer</module> <class>Scommerce_Customer_Model_Entity_Setup</class> </setup> <connection> <use>core_setup</use> </connection> </scommercecustomer_setup> </resources> </global>
</config>

The above declaration will help us creating our sql file to create new attribute. Obviously it will be extending Magento Core Customer Set up class i.e. Mage_Customer_Model_Entity_Setup.

Step 4 – Create set up class in appcodelocalScommerceCustomerEntitySetup.php using the following code

class Scommerce_Customer_Model_Entity_Setup extends Mage_Customer_Model_Entity_Setup
{ }

Step 5 – Final step is to create an attribute(s) in appcodelocalScommerceCustomersqlscommercecustomer_setup/mysql4-install.0.0.1.php file using the following code

/** * Creating custom attributes */ /* @var $installer Scommerce_Customer_Model_Entity_Setup */
$installer = $this; $installer->startSetup(); $installer->addAttribute('customer', 'new_field_name', array( 'user_defined' => 1, 'label' => 'New Field Label Name', 'type' => 'int', 'input' => 'text', 'visible' => false, 'required' => false, )); $installer->endSetup();

That’s it, it is as simple as that. Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.

Similar Posts