Friday 17 August 2012

How to Add Custom Field in the One Page Checkout in Magento

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

<li class="wide">
<label for="billing:jobtitle"><?php echo $this->__('Job Title') ?></label>
<div class="input-box">
<input type="text" id="billing:jobtitle" name="billing[jobtitle]" 
value="<?php echo $this->htmlEscape($this->getAddress()->getJobtitle()) ?>" 
title="<?php echo $this->__('Job Title') ?>" class="input-text" />
</div>
</li>
 
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

<li class="wide">
<label for="shipping:jobtitle"><?php echo $this->__('Job Title') ?></label>
<div class="input-box">
<input type="text" id="shipping:jobtitle" name="shipping[jobtitle]" 
value="<?php echo $this->htmlEscape($this->getAddress()->getJobtitle()) ?>"
 title="<?php echo $this->__('Job Title') ?>" class="input-text" onchange="shipping.setSameAsBilling(false);" />
</div>
</li>
 
Step: – 3
Add Custom Field into Customer Address Edit tab in Frontend.
app\design\frontend\[Package_name]\[Theme_name]\template\customer\address\edit.phtml

<li class="wide">
<label for="jobtitle"><?php echo $this->__('Job Title') ?></label>
<div class="input-box">
<input type="text" name="jobtitle" id="jobtitle" title="<?php echo $this->__('Job Title') ?>" 
value="<?php echo $this->htmlEscape($this->getAddress()->getJobtitle()) ?>" class="input-text" />
</div>
</li>
 
Step: – 4

System->Configuration->Customers->Customer Configurations->Address Templates
Add following code in the “Text” Textarea.

{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} 
{{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}
    {{depend company}}{{var company}}{{/depend}}
    {{depend jobtitle}}{{var jobtitle}}{{/depend}}
    {{if street1}}{{var street1}}
    {{/if}}
    {{depend street2}}{{var street2}}{{/depend}}
    {{depend street3}}{{var street3}}{{/depend}}
    {{depend street4}}{{var street4}}{{/depend}}
    {{if city}}{{var city}},  {{/if}}{{if region}}{{var region}}, 
{{/if}}{{if postcode}}{{var postcode}}{{/if}}
    {{var country}}
    T: {{var telephone}}
    {{depend fax}}F: {{var fax}}{{/depend}} 
 
Step: – 5

System->Configuration->Customers->Customer Configurations->Address Templates
Add following code in the “HTML” Textarea.


{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} 
{{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}<br/>
{{depend company}}{{var company}}<br />{{/depend}}
{{depend jobtitle}}{{var jobtitle}}<br />{{/depend}}
{{if street1}}{{var street1}}<br />{{/if}}
{{depend street2}}{{var street2}}<br />{{/depend}}
{{depend street3}}{{var street3}}<br />{{/depend}}
{{depend street4}}{{var street4}}<br />{{/depend}}
{{if city}}{{var city}},  {{/if}}{{if region}}{{var region}}, 
{{/if}}{{if postcode}}{{var postcode}}{{/if}}<br/>
{{var country}}<br/>
{{depend telephone}}T: {{var telephone}}{{/depend}}
{{depend fax}}<br/>F: {{var fax}}{{/depend}} 
 
 
Step: – 6

System->Configuration->Customers->Customer Configurations->Address Templates
Add following code in the “PDF” Textarea.


{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} 
{{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}|
{{depend company}}{{var company}}|{{/depend}}
{{depend jobtitle}}{{var jobtitle}}|{{/depend}}
{{if street1}}{{var street1}}
{{/if}}
{{depend street2}}{{var street2}}|{{/depend}}
{{depend street3}}{{var street3}}|{{/depend}}
{{depend street4}}{{var street4}}|{{/depend}}
{{if city}}{{var city}},  {{/if}}{{if region}}{{var region}}, 
{{/if}}{{if postcode}}{{var postcode}}{{/if}}|
{{var country}}|
{{depend telephone}}T: {{var telephone}}{{/depend}}|
{{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.

<?xml version="1.0" encoding="UTF-8"?>
<config>
 <modules>
  <Tdg_Check>
   <active>true</active>
   <codePool>local</codePool>
  </Tdg_Check>
 </modules>
</config> 
 
 
Step: – 8
app\code\local\Tdg\Check\etc\ config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
 <modules>
  <Tdg_Check>
   <version>1.0.0</version>
  </Tdg_Check>
 </modules>
 <admin>
  <fieldsets>
   <customer_dataflow>
    <jobtitle><billing>1</billing><shipping>1</shipping></jobtitle>
   </customer_dataflow>
  </fieldsets>
 </admin>
 <global>
  <models>
   <check>
    <class>Tdg_Check_Model</class>
   </check>
  </models>
  <resources>
   <check_setup>
    <setup>
     <module>Tdg_Check</module>
    </setup>
    <connection>
     <use>core_setup</use>
    </connection>
   </check_setup>
   <check_write>
    <connection>
     <use>core_write</use>
    </connection>
   </check_write>
   <check_read>
    <connection>
     <use>core_read</use>
    </connection>
   </check_read>
  </resources>
  <fieldsets>
   <sales_copy_order_billing_address>
    <jobtitle><to_order>*</to_order></jobtitle>
   </sales_copy_order_billing_address>
   <sales_copy_order_shipping_address>
    <jobtitle><to_order>*</to_order></jobtitle>
   </sales_copy_order_shipping_address>
   <sales_convert_quote_address>
    <jobtitle><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></jobtitle>
   </sales_convert_quote_address>
   <sales_convert_order_address>
    <jobtitle><to_quote_address>*</to_quote_address></jobtitle>
   </sales_convert_order_address>
   <customer_address>
    <jobtitle><to_quote_address>*</to_quote_address></jobtitle>
   </customer_address>
   <checkout_onepage_billing>
    <jobtitle><to_customer>*</to_customer></jobtitle>
   </checkout_onepage_billing>
  </fieldsets>
 </global>
</config> 
 
Step: – 9
app\code\local\Tdg\Check\sql\check_setup\ mysql4-install-1.0.0.php


<?php
 /* @var $installer Mage_Customer_Model_Entity_Setup */
    $installer = $this;
    $installer->startSetup();
    /* @var $addressHelper Mage_Customer_Helper_Address */
    $addressHelper = Mage::helper('customer/address');
    $store         = Mage::app()->getStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    /* @var $eavConfig Mage_Eav_Model_Config */
    $eavConfig = Mage::getSingleton('eav/config');

    // update customer address user defined attributes data
    $attributes = array(
        'jobtitle'           => array(
            'label'    => 'Job Title',
            'backend_type'     => 'varchar',
            'frontend_input'    => 'text',
            'is_user_defined'   => 1,
            'is_system'         => 0,
            'is_visible'        => 1,
            'sort_order'        => 140,
            'is_required'       => 1,
            'multiline_count'   => 0,
            'validate_rules'    => array(
                'max_text_length'   => 255,
                'min_text_length'   => 1
            ),
        ),
    );

    foreach ($attributes as $attributeCode => $data) {
        $attribute = $eavConfig->getAttribute('customer_address', $attributeCode);
        $attribute->setWebsite($store->getWebsite());
        $attribute->addData($data);
            $usedInForms = array(
                'adminhtml_customer_address',
                'customer_address_edit',
                'customer_register_address'
            );
            $attribute->setData('used_in_forms', $usedInForms);
        $attribute->save();
    }

    $installer->run("
        ALTER TABLE {$this->getTable('sales_flat_quote_address')} 
ADD COLUMN `jobtitle` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
         ALTER TABLE {$this->getTable('sales_flat_order_address')} 
ADD COLUMN `jobtitle` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
        ");
    $installer->endSetup();
?>

24 comments:

  1. it does not save in table and not display in order detail in admin side

    ReplyDelete
  2. "Hello
    Thank you for the post
    It seems like such solution http://amasty.com/order-attributes.html
    can be useful here. Attributes can be placed at any place you want.

    Thank you
    Have a nice day,
    Jim"

    ReplyDelete
  3. Hi,

    Great Help for Add Custom Comment Box Or Field For Each Product On Cart Page And Show Values At Order Detail Page In Magento Admin at following url.

    http://www.webslike.com/Thread-How-To-Add-Custom-Comment-Box-For-Each-Product-On-Cart-Page-Shows-in-Magento-Admin

    ReplyDelete
  4. Not working for me either on Magento 1.7.2
    Please can you advise which version of Magento you have tested this on?

    Cheers,
    Dan

    ReplyDelete
  5. getJobtitle()
    where is this function located?

    ReplyDelete
  6. I have the same questions.

    ReplyDelete
  7. same here - where do you set the getJobtitle() function??

    ReplyDelete
  8. Thanks for sharing this step by step tutorial to add custom checkout fields to improve the efficiency of the checkout page.
    It would be better if you provide this in form a plug-in.

    Also have a look at this Magento Additional Checkout Attributes Extension released by FME: http://www.fmeextensions.com/magento-additional-checkout-attributes-fields.html

    ReplyDelete
  9. Custom field is not save to DB in onepage checkout billing & shipping section. But save via address book.

    ReplyDelete
  10. Thanks for the post hope so that I will be able to execute all these things of my own. People who are really concerned about the looks of their website may take help from the after effects templates that are specially designed according to the purpose of the website.

    ReplyDelete
  11. new field does not into database

    ReplyDelete
  12. Great Read! I am impressed on how you make your article easy to understand. I'll come back for more :D

    offshore magento development

    ReplyDelete
  13. working fine with magento 1.9....really a good post....i recommend to everyone to try this who have not got answer yet....stop 2 day searching journey....thank you so much Abid

    ReplyDelete
  14. Adding custom field to Magento Checkout or order page requires a lot of coding in multiple files. Refer to the following article for complete coding solution.

    http://www.demagento.com/tutorial-magento-add-custom-field-to-order-checkout-page/

    ReplyDelete
  15. It is one of the impressive article and I really enjoyed these services for business. Thank you so much for posting this article.

    custom ecommerce developers

    ReplyDelete
  16. Hello Mr.Abid Ali Tanwar!
    First of all thanks for the great extension.
    And, its working for me when i do as like your steps.
    But, i need to customize it for adding 3 text fields(like job fields). Now, after my customization field values are not saving to db.

    I can't understand where is the mistake. Whether in the cache after updating the extension or in the code i modified.

    Please help me.
    Thanks in advance.

    ReplyDelete
  17. Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post!
    newizze

    ReplyDelete
  18. is this work in magento 1.9?

    ReplyDelete
  19. I read that Post and got it fine and informative. Please share more like that...
    40ft shipping container

    ReplyDelete

Note: only a member of this blog may post a comment.