Thursday, December 04, 2014

How to add custom attribute to Customer in Magento 1.8

For this we need to create a module. Check the article Building a basic Hello World module in Magento 1.8 to know how to build a basic module in Magento 1.8. 

So, now let's checkout the files we need to create this module. I am using my name "Chandan" as the Namespace name for my module and the module name is "ExtraCustomerAttributes". And I am going to add a field called 'isapproved' (Is Approved) to customer entity. With this field we can do various thing which can be discussed later. 

Here is the file list :

1. app/etc/modules/Chandan_ExtraCustomerAttributes.xml 
2. app/code/local/Chandan/ExtraCustomerAttributes/etc/config.xml 
3. app/code/local/Chandan/ExtraCustomerAttributes/sql/customerattribute_setup/mysql4-install-0.1.0.php

The above three files are just what we need to add our custom field to Customer. Now let's check out each of them one by one.

Here is the content for app/etc/modules/Chandan_ExtraCustomerAttributes.xml 

<?xml version="1.0"?>
<config>
  <modules>
    <Chandan_ExtraCustomerAttributes>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Chandan_ExtraCustomerAttributes>
  </modules>
</config>

The file Chandan_ExtraCustomerAttributes.xml tells Magento that we have an active Module "Chandan_ExtraCustomerAttributes" which runs from 'local' code pool. "Chandan" is the namespace name and "ExtraCustomerAttributes" is the name of the module.

Next, let's check the content of app/code/local/Chandan/ExtraCustomerAttributes/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Chandan_ExtraCustomerAttributes>
      <version>0.1.0</version>
    </Chandan_ExtraCustomerAttributes>
  </modules>
  <global>
<resources>
 <customerattribute_setup>
<setup>
 <module>Chandan_ExtraCustomerAttributes</module>
 <class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
 <use>core_setup</use>
</connection>
 </customerattribute_setup>
 <customerattribute_write>
<connection>
 <use>core_write</use>
</connection>
 </customerattribute_write>
 <customerattribute_read>
<connection>
 <use>core_read</use>
</connection>
 </customerattribute_read>
</resources>
  </global>
</config> 

And finally, here is the content of the file app/code/local/Chandan/ExtraCustomerAttributes/sql/customerattribute_setup/mysql4-install-0.1.0.php

This file creates necessary attribute for Customer. 

<?php
$installer = $this;
$installer->startSetup();

// new attribute name is 'isapproved'

// Type Integer
$installer->addAttribute("customer", "isapproved",  array(
    "type"     => "int",
    "backend"  => "",
    "label"    => "Is Approved",  
    "input"    => "select", 
    "source"   => "eav/entity_attribute_source_boolean", 
    "visible"  => true,
    "required" => false,
    "default"  => "No",
    "frontend" => "",
    "unique"   => false,
    "note"     => "To determine whether a customer is approved"

));


$attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "isapproved");


$used_in_forms = array();

$used_in_forms[] = "adminhtml_customer";

        
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 0)
->setData("sort_order", 100)
;

// Save Attribute

$attribute->save();

$installer->endSetup();
?>

When the mysql4-install-0.1.0.php file is executed, it creates entries in various tables. 

First, it created a row in "eav_attribute" table. I had the following row added to "eav_attribute" table. It is column-wise shown below.

attribute_id    => 961
entity_type_id  => 1
attribute_code  => isapproved
attribute_model => NULL
backend_model   => NULL
backend_type    => int
backend_table   => NULL
frontend_model  => NULL 
frontend_input  => select
frontend_label  => Is Approved
frontend_class  => NULL
source_model    => eav/entity_attribute_source_boolean
is_required     => 0
is_user_defined => 1
default_value   => No
is_unique       => 0
note            => To determine whether a customer is approved

In "eav_attribute" table, we have "entity_type_id" set to "1" for Customer; the "attribute_id" is having an auto value. Various entity types can be found in table "eav_entity_type". Customer Address (customer_address) is set to have id "2" and Customer Payment (customer_payment) has an Id of "3". For example, if we add address to a Customer from backend, the table "customer_address_entity" gets a new entry with "entity_type_id" set to "2".

Ok, now getting back to our module, in the file mysql4-install-0.1.0.php, there is a line :

 "source" => "eav/entity_attribute_source_boolean",

The "eav/entity_attribute_source_boolean" provides a selection of "Yes" or "No". 
"customer/entity_address_attribute_source_country" provides a list of Countries.

This section can be modified to accommodate custom values like "Processing...", "No Yet.." against our field/attribute "isapproved". We need to modify our code as shown below ... only the "source" section needs editing...

$installer->addAttribute("customer", "isapproved",  array(
    
     "source" => "eav/entity_attribute_source_table",
     "option"=> array(
                  "values" => array(
                     0 => "Processing...",
                     1 => "Banned Completely",
                     2 => "Not Yet",
     3 => "Approved",
4 => "Not Approved"
                   ),
                )
));

Notice that we have used "eav/entity_attribute_source_table" as "source" here. In this case, "eav_attribute_option" and "eav_attribute_option_value" tables would have entries to hold the configuration. The table "eav_attribute_option_value" would be storing the values "Processing...", "Not Yet" etc.

The table "eav_entity_attribute" also gets an entry with "attribute_id" set to 961 (reference to 'eav_attribute' table) and 'entity_type_id' set to "1". 

The table "customer_form_attribute" gets an entry with "attribute_id" set to 961 and "form_code" set to "adminhtml_customer" as specified in the statement above :

$used_in_forms[] = "adminhtml_customer";

Here, we define the form where the attribute to be saved.

We also get an entry in table "customer_eav_attribute".

Now, let's check whether our module is working or not. First of all, Magento would show our new module in the Module List. From System > Configuration > Advanced, I saw the following ::


Next, from Customers > Manage Customers at top menu, I opened a customer in Edit mode and I was able to see our newly created attribute with a label "Is Approved" as shown below ...



Hope this helps.

3 comments:

Anonymous said...

Thanks for the post. However, I tried to implement the code (I'm on Magento 1.9) but either I'm not good at coding (I'm sure I'm not) or the code doesn't work in 1.9.
Alternatively, I found Customer Attributes by Amasty, this extension helped to complete adding custom fields to registration.

Unknown said...

Thank you very much. It helps me a a a lot.

Unknown said...

how to get custom attribute value