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

Sometimes we need to add the custom fields in to the Onepage checkout process, here I am adding a custom field "comments" into the "Shipping Information" tab of Onepage checkout process and storing the values into the tables
"sales_flat_quote_address" & "sales_flat_order_address".

Please see the example below for better understanding:

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

<li class="control">
  <div>
     <label for="shipping:comments"><?php echo $this->__('Comments') ?  ></label><br />
    <input type="text"  name="shipping[comments]" id="comments" title="Comments" value="" class="input-  text" maxlength="20" />
 </div>
</li>

Creating custom module name as "Customization"
Step2: Create app\etc\modules\Namespace_ Customization.xml
<Namespace_Customization>
            <active>true</active>
            <codePool>local</codePool>
</Namespace_Customization>

Step3: create a xml file
app\code\local\Namespace\Customization\etc\ config.xml
<config>
    <modules>
        <Namespace_Customization>
            <version>0.1.0</version>
        </Namespace_Customization>
    </modules>
    <global>
  <models>
            <namespace_customization>
                <class>Namespace_Customization_Model</class>
                <resourceModel>namespace_customization_mysql4</resourceModel>
            </namespace_customization>
   <namespace_customization_mysql4>
    <class>Namespace_Customization_Model_Mysql4</class>
   </namespace_customization_mysql4> 
        </models>
  <resources>
            <namespace_customization_setup>
                <setup>
                    <module>Namespace_Customization</module>
     <class>Namespace_Customization_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </namespace_customization_setup>
            <namespace_customization_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </namespace_customization_write>
            <namespace_customization_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </namespace_customization_read>
        </resources>
  <blocks>
            <namespace_customization><class>Namespace_Customization_Block</class></namespace_customization>
        </blocks>
        <helpers>
            <namespace_customization><class>Namespace_Customization_Helper</class></namespace_customization>
        </helpers>      
 <fieldsets>
      
<sales_convert_quote_address>
   <comments><to_order_address>*</to_order_address></comments >
  </sales_convert_quote_address>
  <sales_convert_order_address>
  < comments ><to_quote_address>*</to_quote_address></comments >
              </sales_convert_order_address>
 </fieldsets>
    </global>
    <default>
        <cart>
            <email>
                <email_identity>general</email_identity>
                <email_template>cart_email_email_template</email_template>
            </email>
        </cart>
    </default>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <namespace_customization after="Mage_Checkout">Namespace_Customization</namespace_customization>
                    </modules>
                </args>
            </checkout>
        </routers>
        <routers>
            <customization>
                <use>standard</use>
                <args>
                    <module>Namespace_Customization</module>
                    <frontName>customization</frontName>
                </args>
            </customization>
        </routers>
 
    </frontend> 
</config>

Step4: create a sql setup file app\code\local\Namespace\Customization\sql\namespace_customization_setup\mysql4-install-0.1.0.php
Add the content below in file.

<?php
$installer = $this;
/* @var $installer Mage_Sales_Model_Mysql4_Setup */

$entityAttributesCodes = array(
    'comments' => 'varchar'  
);
foreach ($entityAttributesCodes as $code => $type) {
    $installer->addAttribute('quote_address', $code, array('type' => $type, 'default' => '0', 'user_defined' => false));
    $installer->addAttribute('order_address', $code, array('type' => $type, 'default' => '0', 'user_defined' => false));
    }

Step 5: Override the model file into your local app\code\local\Namespace\Checkout\Model\Type\Onepage.php
and add the below content.

class Namespace_Checkout_Model_Type_Onepage extends Mage_Checkout_Model_Type_Onepage
{
    /**
     * Save checkout shipping address
     *
     * @param   array $data
     * @param   int $customerAddressId
     * @return  Mage_Checkout_Model_Type_Onepage
     */
    public function saveShipping($data, $customerAddressId)
    {
        if (empty($data)) {
            return array('error' => -1, 'message' => $this->_helper->__('Invalid data.'));
        }
        $address = $this->getQuote()->getShippingAddress();
        ...................
            .....................Intermediate code ........
                ........................
             
        if (!empty($customerAddressId)) {
            $customerAddress = Mage::getModel('customer/address')->load($customerAddressId);
            if ($customerAddress->getId()) {
              ...................
                ..................... Intermediate code ........
                ........................
                if ($addressErrors !== true) {
                    return array('error' => 1, 'message' => $addressErrors);
                }
                // comments: set comments field value              
                $address->setSpecDelComment2(empty($data['comments']) ? '' : $data['comments']);          
            }
        } else {
            $addressForm->setEntity($address);
            ...................
                ..................... Intermediate code ........
                ........................
            $addressForm->compactData($addressData);          
            // comments: set comments filed value
            $address->setSpecDelComment2(empty($data['comments']) ? '' : $data['comments']);
        }

        ...................
                ..................... Intermediate code ........
                ........................
        $this->getQuote()->collectTotals()->save();
        ...................
                ..................... Intermediate code ........
                ........................

        return array();
    }

Step 6: Refresh the Cache & ur done.









How to Add Custom Field in the Shipping Address of Onepage Checkout in Magento How to Add Custom Field in the Shipping Address of Onepage Checkout in Magento Reviewed by Web Technology Funda on 6:37:00 AM Rating: 5

No comments

Free! Free!Free! Subscribe to Get Free PHP (Magento) tutorial Update in Your Inbox!!! Hurry Up!!!