Checkout Add Extra Address Field Magento 1.9+

In this blog post, we will see how to add a new address field to magento checkout page. We will also display this field in all magento order pages.

Lets look step by step on how to add an address field.

Step1: Creating Attribute

First we need to create an “customer_address” type attribute and assign it to form code “customer_address_edit”.
We will write this code in setup class of the module to do this make this change in your config.xml file

                 <setup>
                    <module>Excellence_Address</module>
                    <class>Mage_Customer_Model_Resource_Setup</class>
                </setup>

next in your module setup file add this code

$entityTypeId = $setup->getEntityTypeId('customer_address');
$attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

//this is for creating a new attribute for customer address entity
$setup->addAttribute("customer_address", "delivery_instruction", array(
    "type" => "varchar",
    "backend" => "",
    "label" => "Delivery Instruction",
    "input" => "text",
    "source" => "",
    "visible" => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique" => false,
    "note" => "Custom Attribute Will Be Used Show Delivery Instruction Field In Checkout Page"
));

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

$setup->addAttributeToGroup(
        $entityTypeId, $attributeSetId, $attributeGroupId, 'delivery_instruction', '999'  //sort_order
);

$used_in_forms = array();

//$used_in_forms[]="adminhtml_customer";
//$used_in_forms[]="checkout_register";
//$used_in_forms[]="customer_account_create";
$used_in_forms[] = "customer_address_edit"; //this form code is used in checkout billing/shipping address
//$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();

Step2: Add Textfield to Checkout Page

To add field to the billing step of checkout page is easier. In the billing.phtml file, magento already has this code

<?php echo $this->getChildHtml('form.additional.info'); ?>

So we just need to add our block, as child to this and it will automatically get added. So in your module layout xml file add the code

<?xml version="1.0"?>
<layout version="0.1.0">
    <checkout_onepage_index>
        <reference name='checkout.onepage.billing'>
            <block type='checkout/onepage_billing' name='form.additional.info' template='address/checkout/onepage/billing_field.phtml'></block>
        </reference>
    </checkout_onepage_index>
</layout> 

and create phtml file name billing_field.phtml

Adding field to shipping address is not as elegant, we need to overwrite the entire shipping.phtml and create our version if it with the field added.

<div class="field">
    <label for="billing:delivery_instruction" class="required">
        <?php echo $this->__('Delivery Instructions') ?>
    </label>
    <div class="input-box">
        <input type="text" name="billing[delivery_instruction]" id="billing:delivery_instruction" value="<?php echo $this->escapeHtml($this->getAddress()->getDeliveryInstruction()) ?>" title="<?php echo $this->__('Delivery Instructions') ?>" class="input-text required-entry" />
    </div>
</div>

The above code should add the field to checkout page now.

Step3: Adding database fields to quote and order tables

Now we need to store this data in your database tables. The field will be stored in “sales_flat_quote_address” and “sales_flat_order_address” tables. We need to add the respective columns through setup files.

/**
 * Adding Extra Column to sales_flat_quote_address
 * to store the delivery instruction field
 */
$sales_quote_address = $installer->getTable('sales/quote_address');
$installer->getConnection()
        ->addColumn($sales_quote_address, 'delivery_instruction', array(
            'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
            'comment' => 'New Delivery Instruction Field Added'
        ));

/**
 * Adding Extra Column to sales_flat_order_address
 * to store the delivery instruction field
 */
$sales_order_address = $installer->getTable('sales/order_address');
$installer->getConnection()
        ->addColumn($sales_order_address, 'delivery_instruction', array(
            'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
            'comment' => 'New Delivery Instruction Field Added'
        ));

This will add extra column to the database tables.
But we need to do another important thing, which need tell magento to copy the field from quote address object to order address object. This is done via the config.xml file

     <global>
        <fieldsets>
            <sales_convert_quote_address>
                <delivery_instruction>
                    <to_order_address>*</to_order_address>
                </delivery_instruction>
            </sales_convert_quote_address>
        </fieldsets>
     </global>

At this stage when you place an order, address information should show up in both order and quote address database tables.

Step5: Make Changes to Address Display

In the end we need to show the new field , where address is displayed.
Magento use address templates to display address information everywhere, so we just need to add this field to the address templates. Address templates are located in System Configuration -> Customer Configuration

We will add this code in our setup scripts

$config = Mage::getModel('core/config');

//append delivery instruction to address templates in system configuration
$html = Mage::getConfig()->getNode('default/customer/address_templates/html');
$html .= '{{depend delivery_instruction}}<br/>DH:{{var delivery_instruction}} {{/depend}}';
$config->saveConfig('customer/address_templates/html', $html);

$text = Mage::getConfig()->getNode('default/customer/address_templates/text');
$text .= '{{depend delivery_instruction}}DT:{{var delivery_instruction}} {{/depend}}';
$config->saveConfig('customer/address_templates/text', $text);

$oneline = Mage::getConfig()->getNode('default/customer/address_templates/oneline');
$oneline .= '{{depend delivery_instruction}}DO:{{var delivery_instruction}} {{/depend}}';
$config->saveConfig('customer/address_templates/oneline', $oneline);

$pdf = Mage::getConfig()->getNode('default/customer/address_templates/pdf');
$pdf .= '{{depend delivery_instruction}}<br/>DP:{{var delivery_instruction}} {{/depend}}';
$config->saveConfig('customer/address_templates/pdf', $pdf);

$js_template = Mage::getConfig()->getNode('default/customer/address_templates/js_template');
$js_template .= '{{depend delivery_instruction}}<br/>DJ:{{var delivery_instruction}} {{/depend}}';
$config->saveConfig('customer/address_templates/js_template', $js_template);

Above we have added delivery information to all existing address fields.

This are all the steps required.
Doing these you can add fields to checkout billing/shipping steps as you wish.
And make these fields show up in admin order, frontend order information pages.

Entire Source Code for this module is available at:
https://github.com/manishiitg/excellenceblog_checkout_address

  • Visakh Viswanath

    Hello Excellence Technologies,

    My magento version is 1.7.0.2, is it possible to use this code in my version. ? Thanks.

  • Pankaj Upadhyay Sandilya

    how to add extra field

  • wft

    how to add upload button on shipping cart page