Add Custom Field to Customer Address

In this tutorial, we will see how to add custom fields to magento customer address.

In this tutorial we will add a field called ‘Govt ID#’ to address in admin, customer registration form, customer edit address and checkout pages.
Let start step by step, first will create an attribute and show it in admin. The module used by me here is name Excellence_Address. You can download the module createe here
[dm]24[/dm]

Adding Field to Admin

First we need to create the address attribute. There are two ways to do this one is through your module sql file or you can do it through directly through sql query.
First we will see it through the module.
In your module’s config.xml file, add this in the resource tag.

<address_setup>
                <setup>
                    <module>Excellence_Address</module>
                    <class>Mage_Customer_Model_Entity_Setup</class> <!-- This is the important thing-->
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </address_setup>

The important thing is to have your module’s setup class pointed to Mage_Customer_Model_Entity_Setup.
Next in the module’s mysql-install file we will put in the code

$installer = $this;

$installer->startSetup();

$this->addAttribute('customer_address', 'govt_id', array(
	'type' => 'varchar',
	'input' => 'text',
	'label' => 'Govt ID No#',
	'global' => 1,
	'visible' => 1,
	'required' => 0,
	'user_defined' => 1,
	'visible_on_front' => 1
));
Mage::getSingleton('eav/config')
	->getAttribute('customer_address', 'govt_id')
	->setData('used_in_forms', array('customer_register_address','customer_address_edit','adminhtml_customer_address'))
	->save();
$installer->endSetup(); 

This will add the relevant database entries. If you want to do it directly i.e through database or phpmyadmin. Then you need to do the below.
eav_attribute table
insert a row in this table with values entity_type_id = 2, attribute_code = govt_id, backend_type = varchar, fontend_input = text, fontend_label = Govt ID No#, is_user_defined = 1, is_required = 1, default = NULL.
eav_entity_attribute table
insert a row in this table with values entity_type_id = 2,attribute_set_id =2, attribute_group_id = 2, attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table)
customer_eav_attribute table
insert a row in this table with values attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table), is_visible = 1 rest all column will take default values
customer_form_attribute table
1. insert row in this table with values form_code = adminhtml_customer_address and attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table). This required for the attribute to show up in the admin
2. insert row in this table with values form_code = customer_address_edit and attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table). This required for the attribute to get saved in edit address form and checkout page
3. insert row in this table with values form_code = customer_register_address and attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table). This is required to save the attribute in the create account page
After doing this step, you should be able to do see your new address attribute in the admin.

Admin Address Attribute

Admin Address Attribute

Address Format

Next we need to change the address display format, this format is used everywhere in magento where ever address is displayed. To change the format, go to System -> Configuration -> Customer Configuration -> Address Template
There you will see 5 options, we need to change all.
Text
Add {{depend govt_id}}ID# {{var govt_id}}{{/depend}} where ever you want it. {{depend}} basically checks, if govt_id is not empty.
Text One line
Add {{depend govt_id}}ID# {{var govt_id}}{{/depend}} where ever you want it. This format shows up in the checkout page shipping,billing address dropdowns.
HTML
Add {{depend govt_id}}<br/>ID# {{var govt_id}}{{/depend}}. This format is used in many places like Order View, Address Display etc.
PDF
Add {{depend govt_id}}<br/>ID# {{var govt_id}}{{/depend}}|. This format is used in PDF invoices, shipments etc.
Javascript Template
Add <br/>ID#{govt_id}. This is used in admin add/edit address area.
After saving these in the configuration, the new address format should be visible. You can view this here

New Address Format

New Address Format

Create Account Address

First to show address in the magento create account form we need to make changes to customer\form\register.phtml (magento 1.6- version) or persistent\customer\form\register.phtml (magento 1.6+ version). If your using a module we will overwrite these files and make changes, if your not using a module you can make these changes directly. To overwrite, in your module’s layout xml file

<customer_account_create>
    	<reference name='customer_form_register'>
			<action method='setTemplate'><template>address/persistent/customer/form/register.phtml</template></action>
    	</reference>
    </customer_account_create>

Next in the phtml file regsiter.phtml created we will copy the default magento register.phtml contents and add our new field.
First in the beginning of the file add this code, which will show all the address fields.

<?php $this->setShowAddressFields(true);?>

Next we need to add our new field, place this code where you want to show the new field

<li class="fields">
                    <div class="field">
                        <label for="govt_id" class="required"><em>*</em><?php echo $this->__('Govt ID#') ?></label>
                        <div class="input-box">
                            <input type="text" name="govt_id" value="<?php echo $this->htmlEscape($this->getFormData()->getGovtId()) ?>" title="<?php echo $this->__('Govt ID#') ?>" id="govt_id" class="input-text required-entry" />
                        </div>
                    </div>
                </li>

After this the new field added should be automatically saved to the database. You can view the new field in the admin and my account area.

My Account Address Area

My Account Address Area

Edit Address

Next, in the My Account -> Edit Address form we need to add the the new field as well. For this we need to edit the customer\address\edit.phtml file. In the module’s layout xml file put in this code

 <customer_address_form>
    	<reference name='customer_address_edit'>
    		<action method='setTemplate'><template>address/customer/address/edit.phtml</template></action>
    	</reference>
    </customer_address_form>

and in the new edit.phtml file put in the form field where every required

<li class="fields">
                 <div class="field">
                        <label for="govt_id" class="required"><em>*</em><?php echo $this->__('Govt ID#') ?></label>
                        <div class="input-box">
                            <input type="text" name="govt_id" value="<?php echo $this->htmlEscape($this->getAddress()->getGovtId()) ?>" title="<?php echo $this->__('Govt ID#') ?>" id="govt_id" class="input-text required-entry" />
                        </div>
                    </div>
                </li>

Now editing/saving the Govt ID will work.

Checkout Billing/Shipping Step

Now, we will see what we need to do for adding the fields in shipping and billing steps of checkout page.
For this need to make changes to the checkout/onepage/billing.phtml and checkout/onepage/shipping.phtml files. To do this in the module’s layout xml file put in this code

<checkout_onepage_index>
    	<reference name='checkout.onepage.billing'>
    		<action method='setTemplate'><template>address/checkout/onepage/billing.phtml</template></action>
    	</reference>
    	<reference name='checkout.onepage.shipping'>
    		<action method='setTemplate'><template>address/checkout/onepage/shipping.phtml</template></action>
    	</reference>
    </checkout_onepage_index>

Next in the shipping.phtml and billing.phtml files need to add the field. Add this code in billing.phtml file

<li class="fields">
                    <div class="field">
                        <label for="govt_id" class="required"><em>*</em><?php echo $this->__('Govt ID#') ?></label>
                        <div class="input-box">
                            <input type="text" name="billing[govt_id]" value="<?php echo $this->htmlEscape($this->getAddress()->getGovtId()) ?>" title="<?php echo $this->__('Govt ID#') ?>" id="billing:govt_id" class="input-text required-entry" />
                        </div>
                    </div>
                </li>

and add this code in shipping.phtml file

<li class="fields">
	                    <div class="field">
	                        <label for="govt_id" class="required"><em>*</em><?php echo $this->__('Govt ID#') ?></label>
	                        <div class="input-box">
	                            <input type="text" name="shipping[govt_id]" value="<?php echo $this->htmlEscape($this->getAddress()->getGovtId()) ?>" title="<?php echo $this->__('Govt ID#') ?>" id="shipping:govt_id" class="input-text required-entry" />
	                        </div>
	                    </div>
	                </li>

Next, we need to add this xml code to our module config.xml file

<global>
        <fieldsets>
        	<sales_convert_quote_address>
        		<govt_id>
                    <to_order_address>*</to_order_address>
                    <to_customer_address>*</to_customer_address>
                </govt_id>
        	</sales_convert_quote_address>
        	<customer_address>
        		<govt_id>
                    <to_quote_address>*</to_quote_address>
                </govt_id>
        	</customer_address>
        </fieldsets>
    </global>

Next we need to add govt_id column in the tables sales_flat_order_address, sales_flat_quote_address. To do this in your module’s sql file

$tablequote = $this->getTable('sales/quote_address');
$installer->run("
ALTER TABLE  $tablequote ADD  `govt_id` varchar(255) NOT NULL
");

$tablequote = $this->getTable('sales/order_address');
$installer->run("
ALTER TABLE  $tablequote ADD  `govt_id` varchar(255) NOT NULL
");

Now, if a new address is created from the checkout step the new field will be added to the database and show up the order view.

These are all the places where the address shows up. The new field we have added will automatically show up in emails, pdfs etc.