Add custom customer attribute in magento

How to add custom customer attribute in magento?

In this extension we created three customer attribute (i) “Pan Number” which is must be required.
(ii) “Father Name” which is not required.
(iii) “Mother Name” which is not required.

Note:- you can download extension from this rar_image
Add custom customer attribute in magento through custom module.
Note:- Firstly disable the cache from Cache Management tab and which is in System tab and select the rwd theme.
there are many steps to create the extension.
Step1:- Create Mywork_AddCustomCustomerAttribute.xml file in app/etc/modules/.
where Mywork is Namespace and AddCustomCustomerAttribute is Modulename.

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

Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/AddCustomCustomerAttribute/etc/

<?xml version="1.0"?>
<config>
  <modules>
    <Mywork_AddCustomCustomerAttribute>
      <version>1.0.0</version>
    </Mywork_AddCustomCustomerAttribute>
  </modules>
  <global>
    <helpers>
      <AddCustomCustomerAttribute>
        <class>Mywork_AddCustomCustomerAttribute_Helper</class>
      </AddCustomCustomerAttribute>
    </helpers>
	<models>
	  <AddCustomCustomerAttribute>
		<class>Mywork_AddCustomCustomerAttribute_Model</class>
		<resourceModel>AddCustomCustomerAttribute_mysql4</resourceModel>
	  </AddCustomCustomerAttribute>
	</models>
	<resources>
	  <AddCustomCustomerAttribute_setup>
		<setup>
		  <module>Mywork_AddCustomCustomerAttribute</module>
		  <class>Mage_Customer_Model_Entity_Setup</class>
		</setup>
		<connection>
		  <use>core_setup</use>
		</connection>
	  </AddCustomCustomerAttribute_setup>
	  <AddCustomCustomerAttribute_write>
		<connection>
		  <use>core_write</use>
		</connection>
	  </AddCustomCustomerAttribute_write>
	  <AddCustomCustomerAttribute_read>
		<connection>
		  <use>core_read</use>
		</connection>
	  </AddCustomCustomerAttribute_read>
	</resources>
	
	<fieldsets>
        <checkout_onepage_quote>
            <pan_number>
                <to_customer>*</to_customer>
            </pan_number>
	    <father_name>
                <to_customer>*</to_customer>
            </father_name>
	    <mother_name>
                <to_customer>*</to_customer>
            </mother_name>
        </checkout_onepage_quote>
        <customer_account>
            <pan_number>
                <to_quote>*</to_quote>
            </pan_number>
			<father_name>
                <to_quote>*</to_quote>
            </father_name>
			<mother_name>
                <to_quote>*</to_quote>
            </mother_name>
        </customer_account>
    </fieldsets>
</global>
</config> 

Note:- If you want to add custom fields in the “checkout_register” form then use

<fieldsets>
        <checkout_onepage_quote>
            <pan_number>
                <to_customer>*</to_customer>
            </pan_number>
	    <father_name>
                <to_customer>*</to_customer>
            </father_name>
	    <mother_name>
                <to_customer>*</to_customer>
            </mother_name>
        </checkout_onepage_quote>
        <customer_account>
            <pan_number>
                <to_quote>*</to_quote>
            </pan_number>
			<father_name>
                <to_quote>*</to_quote>
            </father_name>
			<mother_name>
                <to_quote>*</to_quote>
            </mother_name>
        </customer_account>
    </fieldsets>


other wise not.

Step3:- Now create the mysql4-install-1.0.0.php file in app/code/local/Mywork/AddCustomCustomerAttribute/sql/AddCustomCustomerAttribute_setup/

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


$installer->addAttribute("customer", "pan_number",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Pan Number",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => true,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => "Customer Pan Number like 1236547898"

	));

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

        
$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
        $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", 1)
		->setData("sort_order", 100)
		;
        $attribute->save();
	
	
	

$installer->addAttribute("customer", "father_name",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Father Name",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

	));

$attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "father_name");
  
$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
        $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", 1)
		->setData("sort_order", 101);
        $attribute->save();
	
	
	

$installer->addAttribute("customer", "mother_name",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Mother Name",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

	));

$attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "mother_name");       
$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
        $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", 1)
		->setData("sort_order", 102)
		;
        $attribute->save();
	
$connection = $installer->getConnection();
$quote_table_name = $installer->getTable('sales/quote');

$connection->addColumn($quote_table_name, 'pan_number', array(
    'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
    'length' => 255,
    'nullable' => false,
    'comment' => 'Pan Number Data',
));	

$connection->addColumn($quote_table_name, 'father_name', array(
    'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
    'length' => 255,
    'nullable' => false,
    'comment' => 'Father Name',
));	

$connection->addColumn($quote_table_name, 'mother_name', array(
    'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
    'length' => 255,
    'nullable' => false,
    'comment' => 'Mother Name',
));	
	
$installer->endSetup();

Note:- If you want to add custom fields in the “checkout_register” form then use

$connection = $installer->getConnection();
$quote_table_name = $installer->getTable('sales/quote');

$connection->addColumn($quote_table_name, 'pan_number', array(
    'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
    'length' => 255,
    'nullable' => false,
    'comment' => 'Pan Number Data',
));	

$connection->addColumn($quote_table_name, 'father_name', array(
    'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
    'length' => 255,
    'nullable' => false,
    'comment' => 'Father Name',
));	

$connection->addColumn($quote_table_name, 'mother_name', array(
    'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
    'length' => 255,
    'nullable' => false,
    'comment' => 'Mother Name',
));

otherwise not.

Step4:- Now create the Data.php file in app/code/local/Mywork/AddCustomCustomerAttribute/Helper/

<?php
class Mywork_AddCustomCustomerAttribute_Helper_Data extends Mage_Core_Helper_Abstract
{
}

Now we are going to design steps

(1) register.phtml which is in the app/design/frontend/rwd/default/template/persistent/customer/form/

      <!-------------------------------Pan Number------------------------------->
		<li>
                    <label for="pan_number" class="required"><em>*</em><?php echo $this->__('Pan Number') ?></label>
                    <div class="input-box">
                        <input type="text" autocapitalize="off" autocorrect="off" spellcheck="false" name="pan_number" id="pan_number" value="<?php echo $this->escapeHtml($this->getFormData()->getPanNumber()) ?>" title="<?php echo $this->__('Pan Number') ?>" class="input-text required-entry" />
                    </div>
                </li>
				
		<!-------------------------------Pan Number------------------------------->
				
		<!-------------------------------Father Name------------------------------->
		<li>
                    <label for="father_name"><?php echo $this->__('Father Name') ?></label>
                    <div class="input-box">
                        <input type="text" autocapitalize="off" autocorrect="off" spellcheck="false" name="father_name" id="father_name" value="<?php echo $this->escapeHtml($this->getFormData()->getFatherName()) ?>" title="<?php echo $this->__('Father Name') ?>" class="input-text" />
                    </div>
                </li>
				
		<!-------------------------------Father Name------------------------------->
				
		<!-------------------------------Mother Name------------------------------->
		<li>
                    <label for="mother_name"><?php echo $this->__('Mother Name') ?></label>
                    <div class="input-box">
                        <input type="text" autocapitalize="off" autocorrect="off" spellcheck="false" name="mother_name" id="mother_name" value="<?php echo $this->escapeHtml($this->getFormData()->getMotherName()) ?>" title="<?php echo $this->__('Mother Name') ?>" class="input-text" />
                    </div>
                </li>
				
	<!-------------------------------Mother Name------------------------------->

Note:- Add this code after

 <ul class="form-list">
                <li class="fields">
                    <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getFormData())->setForceUseCustomerAttributes(true)->toHtml() ?>
                </li>

(2) edit.phtml which is in the app/design/frontend/rwd/default/template/customer/form/

<!-------------------------------Pan Number------------------------------->
		<li>
                    <label for="pan_number" class="required"><em>*</em><?php echo $this->__('Pan Number') ?></label>
                    <div class="input-box">
                        <input type="text" autocapitalize="off" autocorrect="off" spellcheck="false" name="pan_number" id="pan_number" value="<?php echo $this->escapeHtml($this->getCustomer()->getPanNumber()) ?>" title="<?php echo $this->__('Pan Number') ?>" class="input-text required-entry" />
                    </div>
                </li>
				
				<!-------------------------------Pan Number------------------------------->
				
				<!-------------------------------Father Name------------------------------->
		<li>
                    <label for="father_name"><?php echo $this->__('Father Name') ?></label>
                    <div class="input-box">
                        <input type="text" autocapitalize="off" autocorrect="off" spellcheck="false" name="father_name" id="father_name" value="<?php echo $this->escapeHtml($this->getCustomer()->getFatherName()) ?>" title="<?php echo $this->__('Father Name') ?>" class="input-text" />
                    </div>
                </li>
				
				<!-------------------------------Father Name------------------------------->
				
				<!-------------------------------Mother Name------------------------------->
		<li>
                    <label for="mother_name"><?php echo $this->__('Mother Name') ?></label>
                    <div class="input-box">
                        <input type="text" autocapitalize="off" autocorrect="off" spellcheck="false" name="mother_name" id="mother_name" value="<?php echo $this->escapeHtml($this->getCustomer()->getMotherName()) ?>" title="<?php echo $this->__('Mother Name') ?>" class="input-text" />
                    </div>
                </li>
				
				<!-------------------------------Mother Name------------------------------->

Add this code after

<ul class="form-list">
            <li class="fields">
                <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
            </li>

(3) billing.phtml which is in the app/design/frontend/rwd/default/template/persistent/checkout/onepage/form/

<!-------------------------------Pan Number------------------------------->
		<li class="fields">
				<div class="field">
                    <label for="billing:pan_number" class="required"><em>*</em><?php echo $this->__('Pan Number') ?></label>
                    <div class="input-box">
                        <input type="text" name="billing[pan_number]" id="billing:pan_number" value="<?php echo $this->getQuote()->getCustomer()->getPanNumber(); ?>" title="<?php echo $this->__('Pan Number') ?>" class="input-text required-entry" />
                    </div>
				 </div>
                </li>
				
				<!-------------------------------Pan Number------------------------------->
				
				<!-------------------------------Father Name------------------------------->
		<li class="fields">
				<div class="field">
                    <label for="billing:father_name"><?php echo $this->__('Father Name') ?></label>
                    <div class="input-box">
                        <input type="text" name="billing[father_name]" id="billing:father_name" value="<?php echo $this->getQuote()->getCustomer()->getFatherName();  ?>" title="<?php echo $this->__('Father Name') ?>" class="input-text" />
                    </div>
				 </div>
                </li>
				
				<!-------------------------------Father Name------------------------------->
				
				<!-------------------------------Mother Name------------------------------->
		  <li class="fields">
				<div class="field">
                    <label for="billing:mother_name"><?php echo $this->__('Mother Name') ?></label>
                    <div class="input-box">
                        <input type="text" name="billing[mother_name]" id="billing:mother_name" value="<?php echo $this->getQuote()->getCustomer()->getMotherName(); ?>" title="<?php echo $this->__('Mother Name') ?>" class="input-text" />
                    </div>
				</div>
                </li>
				
		   <!-------------------------------Mother Name------------------------------->

Add this code after

<li id="billing-new-address-form"<?php if ($this->customerHasAddresses()): ?> style="display:none;"<?php endif; ?> class="scaffold-form">
        <div class="fieldset">
            <input type="hidden" name="billing[address_id]" value="<?php echo $this->getAddress()->getId() ?>" id="billing:address_id" />
            <ul>
                <li class="fields"><?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getAddress()->getFirstname() ? $this->getAddress() : $this->getQuote()->getCustomer())->setForceUseCustomerRequiredAttributes(!$this->isCustomerLoggedIn())->setFieldIdFormat('billing:%s')->setFieldNameFormat('billing[%s]')->toHtml() ?></li>