Bookmark and Share
Check Google Page Rank Locations of visitors to this page
Click to get Free offers

Wednesday, January 6, 2010

Magento - how to add new attributes

To add customiged fields for shop customer..

First We can make a module to extend the functionality of the customer module already in magento. First adding the module xml file to the app/etc/modules/ directory. You need to decide on a module name. In this example we will use the Sarvottom scope, and call the module 'NewCustomer'. Therefore we need to create a file named Sarvottom_NewCustomer.xml and add the following content:
<code>
<config>
<modules>
<Sarvottom_NewCustomer>
<active>true</active>
<codePool>local</codePool>
</Sarvottom_NewCustomer>
</modules>
</config>
</code>

Second to add the above file means that Magento is now aware of a new custom module been added as local module. Now, we need to create the module itself, which must be located in the app/code/local/Sarvottom/NewCustomer directory. Inside this directory, create an etc directory and copying the following file inside:

app/code/core/Mage/NewCustomer/etc/config.xml

Edit the file and change :
<code>
<modules>
<Mage_NewCustomer>
<version>x.x.x</version>
</Mage_NewCustomer>
</modules>

to:

<modules>
<Sarvottom_NewCustomer>
<version>1.0.0</version>
</Sarvottom_NewCustomer>
</modules>
</code>
Third this file default contains two different <fieldsets> - one in <config><admin> and one in <config><global>. At the end of the second one, in the <config><global><fieldsets><customer_account> scope, add:
<code>
<account_no><create>1</create><update>1</update></account_no>
</code>
In this example account_no is the chosen attribute code. The attribute code that you choose here will be important in all the following steps.
Next we need to add the attribute to the NewCustomer Entity Setup Model. To do this, copy the getDefaultEntities method from app/code/core/Mage/Customer/Model/Entity/Setup.php file to a new file, Model/Entity/Setup.php in our module. Put the method inside the following class:

class Sarvottom_NewCustomer_Model_Entity_Setup extends Mage_Customer_Model_Entity_Setup

Then add the following code at the end of the attributes array inside the customer arrray:

'account_no' => array(
'label' => 'Account No',
'visible' => true,
'required' => true,
),

In this case the new attribute is set as compulsory by adding 'required' => true, to the end of that array. If you want the attribute to be optional, remove the required lines.
Next new attribute needs to be added to the Magento database. The best way to do this is to tell Magento to insert it for you. You can either create your own script that uses the Magento code or you can just drop the following code into one of your template files:

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'account_no', array(
'label' => 'Account No',
'type' => 'varchar',
'input' => 'text',
'visible' => true,
'required' => true,
'position' => 1,
));

A brilliant way to add the above code is the <theme>/template/customer/form/register.phtml file since it needs to be edited to add a custom field for the new attribute. Make sure the above code is enclosed with
<code>
<?php
...
?>
</code>
To add the attribute (ie. run this code) view the register new customer page. Once you have viewed the page this code can be removed.

In this example the attribute type is set to varchar and the input is set to text. This needs to be modified to match the requirements of each specific attribute.
Next point, the attribute is installed and it will show up in the backend. We need to modify the frontend, so that the customers can enter values for the custom attribute. There are two fairly similar phtml files:
<code>
<theme>/template/customer/widget/name.phtml
</code>
Now you need to add a new text box in the name.phtml file, since that is what customers see on the register page. Make sure that the ID and name of the new input text box match the attribute code chosen in step 3. In this case it is 'account_no'. The following shows the sample code that adds a new text box with a label:
<code>
<div class="input-box">
<label for="account_no"><?php echo $this->__('Account No') ?><span class="required">*</span></label><br />
<input type="text" name="account_no" id="account_no" value="<?php echo $this->htmlEscape($this->getFormData()->getAccount_No()) ?>" title="<?php echo $this->__('Account No') ?>" class="required-entry input-text" />
</div>
</code>
View blog reactions

5 comments:

sukumar said...

Hi

your are giving six new attributes for customer. I think this new attributes useful for customer and otherwise useful to magento ecommerce.

sukumar
Magento ecommerce.

Sumanta said...

I have completed all the steps, and i take the attribute code as 'location'.It does not show up in the backend. And when i run the customer registration page I get an error "Call to a member function getLocation() on a non-object in C:\wamp\www\magen\app\design\frontend\base\default\template\customer\widget\name.phtml on line 85". Please Help me by replying.

kurt said...

can i see the output of this code?

Reynold Hugh said...

This is the post I was looking for. Thanks for sharing buddy and it is very helpful for me at least.
Magento Templates

Hetal Patel said...

Acesoftech is a Kolkata based web design, web development and search Engine Optimization company providing professional solutions by expert team. Our web design services are not only quality based but also cost effective. Visit this Kolkata web design company