Skip to main content

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>

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.
Unknown 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

Popular posts from this blog

Financial Engineering

Financial Engineering: Key Concepts Financial engineering is a multidisciplinary field that combines financial theory, mathematics, and computer science to design and develop innovative financial products and solutions. Here's an in-depth look at the key concepts you mentioned: 1. Statistical Analysis Statistical analysis is a crucial component of financial engineering. It involves using statistical techniques to analyze and interpret financial data, such as: Hypothesis testing : to validate assumptions about financial data Regression analysis : to model relationships between variables Time series analysis : to forecast future values based on historical data Probability distributions : to model and analyze risk Statistical analysis helps financial engineers to identify trends, patterns, and correlations in financial data, which informs decision-making and risk management. 2. Machine Learning Machine learning is a subset of artificial intelligence that involves training algorithms t...

Wholesale Customer Solution with Magento Commerce

The client want to have a shop where regular customers to be able to see products with their retail price, while Wholesale partners to see the prices with ? discount. The extra condition: retail and wholesale prices hasn’t mathematical dependency. So, a product could be $100 for retail and $50 for whole sale and another one could be $60 retail and $50 wholesale. And of course retail users should not be able to see wholesale prices at all. Basically, I will explain what I did step-by-step, but in order to understand what I mean, you should be familiar with the basics of Magento. 1. Creating two magento websites, stores and views (Magento meaning of website of course) It’s done from from System->Manage Stores. The result is: Website | Store | View ———————————————— Retail->Retail->Default Wholesale->Wholesale->Default Both sites using the same category/product tree 2. Setting the price scope in System->Configuration->Catalog->Catalog->Price set drop-down to...

How to Prepare for AI Driven Career

  Introduction We are all living in our "ChatGPT moment" now. It happened when I asked ChatGPT to plan a 10-day holiday in rural India. Within seconds, I had a detailed list of activities and places to explore. The speed and usefulness of the response left me stunned, and I realized instantly that life would never be the same again. ChatGPT felt like a bombshell—years of hype about Artificial Intelligence had finally materialized into something tangible and accessible. Suddenly, AI wasn’t just theoretical; it was writing limericks, crafting decent marketing content, and even generating code. The world is still adjusting to this rapid shift. We’re in the middle of a technological revolution—one so fast and transformative that it’s hard to fully comprehend. This revolution brings both exciting opportunities and inevitable challenges. On the one hand, AI is enabling remarkable breakthroughs. It can detect anomalies in MRI scans that even seasoned doctors might miss. It can trans...