How to add custom fields in magento

Magento Provides the default customer registration form with very few fields like name,email,pwd etc. But in realtime we need to get more details from the customer during registration. In order to achieve this we may have to create custom module that extends the magento customer module.

Step 1: We Start with Our module activation file first it just specifies the module name its location and Active Status. This file must be placed in app/etc/modules/ directory as app/etc/modules/Mydons_Customer.xml

Step 2: Next we need to create our config.xml file. Here we specify the model class name of our module. Then in the global tag we specify our custom field for our customer. Here iam creating a custom field called mobile.
Our config.xml should be placed in the directory app/code/local/Mydons/Customer/etc/config.xml


Under the resources tag I have added the customer setup class name. The only model class available in this module is Mydons_Customer_Model_Entity_Setup. This class is a subclass of Mage_Customer_Model_Entity_Setup class.

Step 3: Now We need to create a Model Class we specified in the Previous step. This class contains only a single method called getDefaultEntities. You can copy the getDefaultEntities Method from the Base class Mage_Customer_Model_Entity_Setup and paste it in our model class Mydons_Customer_Model_Entity_Setup. This File must reside in our Model directory app/code/local/Mydons/Customer/Model/Entity/Setup.php

Step 4: We are going to add our custom field mobile in the customer array of the getDefaultEntities Method as shown below

Step 5: In Our Final Step we need to create our setup file which is a install script in the directory app/code/local/Mydons_Customer/sql/customerattribute_setup/mysql4-install-0.1.0.php

Here in the first part i have added the customer attribute to EAV table with attribute Properties. For Magento version 1.3 this part alone is enough to make the custom field save to the database. However for latest versions like Magento 1.4 and 1.5 we need an additional step to save in Customer Attribute table. First we get the Eav attribute using its attribute code and then we need to Set in which forms the custom field is going to be used.
After this Step we can add our custom field in the register and account edit template files. The custom field will now be saved to the database.

Compatability : Magento 1.4 and Magento 1.5

猜你喜欢

转载自wuchengyi.iteye.com/blog/1938199
今日推荐