Thursday, 16 August 2012

How to Add Custom Field in the Billing and Shipping Address of Onepage Checkout in Magento

I have created the module for adding the new custom field in the Billing and Shipping address of the Onepage checkout. All the code of the module is given below.
Step: – 1
Add Custom Field into Billing Address tab of the Onepage Checkout in Frontend.
app\design\frontend\[Package_name]\[Theme_name]\template\checkout\onepage\billing.phtml


1
  • class="wide">
  • 2
    3
    class="input-box">
    4"text" id="billing:jobtitle" name="billing[jobtitle]" value="htmlEscape($this->getAddress()->getJobtitle()) ?>" title="__('Job Title') ?>" class="input-text" />
    5
    6
                     

    Step: – 2
    Add Custom Field into Shipping Address tab of the Onepage Checkout in Frontend.
    app\design\frontend\[Package_name]\[Theme_name]\template\checkout\onepage\shipping.phtml


        1
  • class="wide">
  • 2
    3
    class="input-box">
    4"text" id="shipping:jobtitle" name="shipping[jobtitle]" value="htmlEscape($this->getAddress()->getJobtitle()) ?>" title="__('Job Title') ?>" class="input-text" onchange="shipping.setSameAsBilling(false);" />
    5
    6
     

    Step: – 3
    Add Custom Field into Customer Address Edit tab in Frontend.
    app\design\frontend\[Package_name]\[Theme_name]\template\customer\address\edit.phtml



    1
  • class="wide">
  • 2
    3
    class="input-box">
    4"text" name="jobtitle" id="jobtitle" title="__('Job Title') ?>" value="htmlEscape($this->getAddress()->getJobtitle()) ?>" class="input-text" />
    5
    6


    Step: – 4


    I have made some changes to the Address Templates in Magento Admin. if you are using Magento Version 1.3.2.4 then you have to made changes in the following file.
    app/code/core/Mage/Customer/etc/config.xml
    and if you are using Magento Upper Version than 1.3.2.4 then you can chage from the Admin panel of the Magento as given following path.
    System->Configuration->Customers->Customer Configurations->Address Templates
    Add following code in the “Text” Textarea.
    01{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}
    02    {{depend company}}{{var company}}{{/depend}}
    03    {{depend jobtitle}}{{var jobtitle}}{{/depend}}
    04    {{if street1}}{{var street1}}
    05    {{/if}}
    06    {{depend street2}}{{var street2}}{{/depend}}
    07    {{depend street3}}{{var street3}}{{/depend}}
    08    {{depend street4}}{{var street4}}{{/depend}}
    09    {{if city}}{{var city}},  {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}
    10    {{var country}}
    11    T: {{var telephone}}
    12    {{depend fax}}F: {{var fax}}{{/depend}}
    Step: – 5
    I have made some changes to the Address Templates in Magento Admin. if you are using Magento Version 1.3.2.4 then you have to made changes in the following file.
    app/code/core/Mage/Customer/etc/config.xml
    and if you are using Magento Upper Version than 1.3.2.4 then you can chage from the Admin panel of the Magento as given following path.
    System->Configuration->Customers->Customer Configurations->Address Templates
    Add following code in the “HTML” Textarea.
    01{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}<br/>
    02{{depend company}}{{var company}}<br />{{/depend}}
    03{{depend jobtitle}}{{var jobtitle}}<br />{{/depend}}
    04{{if street1}}{{var street1}}<br />{{/if}}
    05{{depend street2}}{{var street2}}<br />{{/depend}}
    06{{depend street3}}{{var street3}}<br />{{/depend}}
    07{{depend street4}}{{var street4}}<br />{{/depend}}
    08{{if city}}{{var city}},  {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}<br/>
    09{{var country}}<br/>
    10{{depend telephone}}T: {{var telephone}}{{/depend}}
    11{{depend fax}}<br/>F: {{var fax}}{{/depend}}


    Step: – 6



    I have made some changes to the Address Templates in Magento Admin. if you are using Magento Version 1.3.2.4 then you have to made changes in the following file.
    app/code/core/Mage/Customer/etc/config.xml
    and if you are using Magento Upper Version than 1.3.2.4 then you can chage from the Admin panel of the Magento as given following path.
    System->Configuration->Customers->Customer Configurations->Address Templates
    Add following code in the “PDF” Textarea.
    01{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}|
    02{{depend company}}{{var company}}|{{/depend}}
    03{{depend jobtitle}}{{var jobtitle}}|{{/depend}}
    04{{if street1}}{{var street1}}
    05{{/if}}
    06{{depend street2}}{{var street2}}|{{/depend}}
    07{{depend street3}}{{var street3}}|{{/depend}}
    08{{depend street4}}{{var street4}}|{{/depend}}
    09{{if city}}{{var city}},  {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}|
    10{{var country}}|
    11{{depend telephone}}T: {{var telephone}}{{/depend}}|
    12{{depend fax}}<br />F: {{var fax}}{{/depend}}|
    I have created the separate module for adding Custom Field into Billing and Shipping Address in Frontend and Backend of the Magento Store.


    Step: – 7


    app\etc\modules\Tdg_All.xml
    First of all create file with above given name and put following code into that file.
    1xml version="1.0" encoding="UTF-8"?>
    2<config>
    3    <modules>
    4        <Tdg_Check>
    5            <active>true</active>
    6            <codePool>local</codePool>
    7        </Tdg_Check>
    8    </modules>
    9</config>
    Step: – 8
    app\code\local\Tdg\Check\etc\ config.xml
    01xml version="1.0" encoding="UTF-8"?>
    02<config>
    03    <modules>
    04        <Tdg_Check>
    05            <version>1.0.0</version>
    06        </Tdg_Check>
    07    </modules>
    08    <admin>
    09        <fieldsets>
    10            <customer_dataflow>
    11                <jobtitle><billing>1</billing><shipping>1</shipping></jobtitle>
    12            </customer_dataflow>
    13        </fieldsets>
    14    </admin>
    15    <global>
    16        <models>
    17            <check>
    18                <class>Tdg_Check_Model</class>
    19            </check>
    20        </models>
    21        <resources>
    22            <check_setup>
    23                <setup>
    24                    <module>Tdg_Check</module>
    25                </setup>
    26                <connection>
    27                    <use>core_setup</use>
    28                </connection>
    29            </check_setup>
    30            <check_write>
    31                <connection>
    32                    <use>core_write</use>
    33                </connection>
    34            </check_write>
    35            <check_read>
    36                <connection>
    37                    <use>core_read</use>
    38                </connection>
    39            </check_read>
    40        </resources>
    41        <fieldsets>
    42            <sales_copy_order_billing_address>
    43                <jobtitle><to_order>*</to_order></jobtitle>
    44            </sales_copy_order_billing_address>
    45            <sales_copy_order_shipping_address>
    46                <jobtitle><to_order>*</to_order></jobtitle>
    47            </sales_copy_order_shipping_address>
    48            <sales_convert_quote_address>
    49                <jobtitle><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></jobtitle>
    50            </sales_convert_quote_address>
    51            <sales_convert_order_address>
    52                <jobtitle><to_quote_address>*</to_quote_address></jobtitle>
    53            </sales_convert_order_address>
    54            <customer_address>
    55                <jobtitle><to_quote_address>*</to_quote_address></jobtitle>
    56            </customer_address>
    57            <checkout_onepage_billing>
    58                <jobtitle><to_customer>*</to_customer></jobtitle>
    59            </checkout_onepage_billing>
    60        </fieldsets>
    61    </global>
    62</config>



    Step: – 9
     

    app\code\local\Tdg\Check\sql\check_setup\ mysql4-install-1.0.0.php
    01
    02    /* @var $installer Mage_Customer_Model_Entity_Setup */
    03    $installer = $this;
    04    $installer->startSetup();
    05    /* @var $addressHelper Mage_Customer_Helper_Address */
    06    $addressHelper = Mage::helper('customer/address');
    07    $store         = Mage::app()->getStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    08 
    09    /* @var $eavConfig Mage_Eav_Model_Config */
    10    $eavConfig = Mage::getSingleton('eav/config');
    11 
    12    // update customer address user defined attributes data
    13    $attributes = array(
    14        'jobtitle'           => array(
    15            'label'    => 'Job Title',
    16            'backend_type'     => 'varchar',
    17            'frontend_input'    => 'text',
    18            'is_user_defined'   => 1,
    19            'is_system'         => 0,
    20            'is_visible'        => 1,
    21            'sort_order'        => 140,
    22            'is_required'       => 1,
    23            'multiline_count'   => 0,
    24            'validate_rules'    => array(
    25                'max_text_length'   => 255,
    26                'min_text_length'   => 1
    27            ),
    28        ),
    29    );
    30 
    31    foreach ($attributes as $attributeCode => $data) {
    32        $attribute = $eavConfig->getAttribute('customer_address', $attributeCode);
    33        $attribute->setWebsite($store->getWebsite());
    34        $attribute->addData($data);
    35            $usedInForms = array(
    36                'adminhtml_customer_address',
    37                'customer_address_edit',
    38                'customer_register_address'
    39            );
    40            $attribute->setData('used_in_forms', $usedInForms);
    41        $attribute->save();
    42    }
    43 
    44    $installer->run("
    45        ALTER TABLE {$this->getTable('sales_flat_quote_address')} ADD COLUMN `jobtitle` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
    46         ALTER TABLE {$this->getTable('sales_flat_order_address')} ADD COLUMN `jobtitle` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
    47        ");
    48    $installer->endSetup();
    49?>

    No comments: