0

I want to add custom fields like whatsapp number etc in onepage checkout billing form and want to hide the shipping steps also.

4
  • 1
    What do you mean by shipping steps i.e you want to hide shipping method and payment method or all your own customized steps ???? In both cases i can help you out Mar 7, 2017 at 6:22
  • no I want to hide the shipping details form,means the second step ,i want o add one more extra field in billing form also
    – developer
    Mar 7, 2017 at 7:31
  • Ok i will help you out for both cases Mar 7, 2017 at 7:47
  • I think the below code will help you . Mar 9, 2017 at 11:04

1 Answer 1

1

Let start step by step, on how to add a single field on the billing step.

Step 1

This is the most simple step. I will add a text field to the billing.phtml file.

For magento 1.6(-) the file to edit is

checkout/onepage/billing.phtml

and for magento 1.6(+) you need to edit file

persistent/checkout/onepage/billing.phtml

. Open the phtml file, find the code

<?php if ($this->canShip()): ?>

and just above the line add this

  <li class="fields">
                    <div class="field">
                        <label for="billing:ssn" class="required"><em>*</em><?php echo $this->__('SSN') ?></label>
                        <div class="input-box">
                            <input type="text" name="custom[ssn]" value="<?php echo $this->htmlEscape($this->getQuote()->getSsn()) ?>" title="<?php echo $this->__('SSN') ?>" class="input-text required-entry" id="billing:ssn" />
                        </div>
                    </div>

                </li>

After doing this, you should see a field in billing.phtml as shown below image

You can add this field on any checkout step, it doesn’t matter for my code

enter image description here


For Hiding the Shipping information from checkout step

Just open the files that was used in above step 1. i.e billing.phtml and find the code for the radio button

 <?php if ($this->canShip()): ?>
        <li class="control">
            <input type="radio" name="billing[use_for_shipping]" id="billing:use_for_shipping_yes" value="1"<?php if ($this->isUseBillingAddressForShipping()) {?> checked="checked"<?php }?> title="<?php echo  Mage::helper('core')->quoteEscape($this->__('Ship to this address')) ?>" onclick="$('shipping:same_as_billing').checked = true;" class="radio" /><label for="billing:use_for_shipping_yes"><?php echo  $this->__('Ship to this address') ?></label></li>
        <li class="control">
            <input type="radio" name="billing[use_for_shipping]" id="billing:use_for_shipping_no" value="0"<?php if (!$this->isUseBillingAddressForShipping()) {?> checked="checked"<?php }?> title="<?php echo Mage::helper('core')->quoteEscape($this->__('Ship to different address')) ?>" onclick="$('shipping:same_as_billing').checked = false;" class="radio" /><label for="billing:use_for_shipping_no"><?php echo $this->__('Ship to different address') ?></label>
        </li>
    <?php endif; ?>

the above code is for setting the default address as same as shipping it will be around line number 184

just Comment the above code and replace it with the below code

<li class="control">
        <input type="hidden" name="billing[use_for_shipping]" value="1" />
    </li>

your radio button will hide now to hide the step from the all steps jus find the which file is using this

extends Mage_Checkout_Block_Onepage_Abstract

or you can search for the below function

public function getSteps()

in the function use this

$stepCodes = array_diff($stepCodes, array('shipping'));

Now, If you have followed all the steps then shipping information will hidden in onepage checkout


For Saving the Custom Field Created above in billing step into database just follow the steps

Save the field in Quote and Order Tables To save, out field to the quote object and database he are the things to do. First i will create a database table “sales_quote_custom”. Put the following sql in your modules mysql_install file

<?php
$installer = $this;
$installer->startSetup();
$installer->run("
CREATE TABLE IF NOT EXISTS {$this->getTable('sales_quote_custom')} (
  `id` int(11) unsigned NOT NULL auto_increment,
  `quote_id` int(11) unsigned NOT NULL,
  `key` varchar(255) NOT NULL,
  `value` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS {$this->getTable('sales_order_custom')} (
  `id` int(11) unsigned NOT NULL auto_increment,
  `order_id` int(11) unsigned NOT NULL,
  `key` varchar(255) NOT NULL,
  `value` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();

We are creating two tables, one for quote and other for order. Now open phpmyadmin and confirm if the quote and order tables are created.

Next we need to write some event observers to capture quote save, and load events. So open your module's config.xml file and inside the tag put in the below code

<events>
     <sales_quote_save_before> <!-- Event Called Before Quote Object is saved -->
         <observers>
                   <save_before>
                    <type>singleton</type>
                    <class>Stack_Custom_Model_Observer</class> <!-- Over Model Class -->
                    <method>saveQuoteBefore</method> <!-- name of function -->
                  </save_before>
        </observers>
      </sales_quote_save_before>
      <sales_quote_save_after> <!-- Event called After Quote Object is saved -->
         <observers>
                   <save_after>
                        <type>singleton</type>
                        <class>Stack_Custom_Model_Observer</class> <!-- Over Model Class -->
                        <method>saveQuoteAfter</method> <!-- name of function -->
                  </save_after>
        </observers>
      </sales_quote_save_after>
      <sales_quote_load_after> <!-- Event called when Quote Object is loaded -->
            <observers>
                <load_after>
                        <type>singleton</type>
                        <class>Stack_Custom_Model_Observer</class> <!-- Over Model Class -->
                        <method>loadQuoteAfter</method> <!-- name of function -->
                 </load_after>
             </observers>
      </sales_quote_load_after>
      <sales_model_service_quote_submit_after> <!-- Event called after order placed -->
            <observers>
                <sales_model_service_quote_submit_after>
                        <type>singleton</type>
                        <class>Stack_Custom_Model_Observer</class> <!-- Over Model Class -->
                        <method>saveOrderAfter</method> <!-- name of function -->
                 </sales_model_service_quote_submit_after>
             </observers>
      </sales_model_service_quote_submit_after>
      <sales_order_load_after> <!-- Event called after Order Object is loaded -->
            <observers>
                <sales_order_load_after>
                        <type>singleton</type>
                        <class>Stack_Custom_Model_Observer</class> <!-- Over Model Class -->
                        <method>loadOrderAfter</method> <!-- name of function -->
                 </sales_order_load_after>
             </observers>
      </sales_order_load_after>
</events>

and next we need to create our Observer classes in Model folder. So create an Observer.php in your modules Model folder.

    <?php
class Stack_Custom_Model_Observer{
    /**
     * This function is called just before $quote object get stored to database.
     * Here, from POST data, we capture our custom field and put it in the quote object
     * @param unknown_type $evt
     */
    public function saveQuoteBefore($evt){
        $quote = $evt->getQuote();
        $post = Mage::app()->getFrontController()->getRequest()->getPost();
        if(isset($post['custom']['ssn'])){
            $var = $post['custom']['ssn'];
            $quote->setSsn($var);
        }
    }
    /**
     * This function is called, just after $quote object get saved to database.
     * Here, after the quote object gets saved in database
     * we save our custom field in the our table created i.e sales_quote_custom
     * @param unknown_type $evt
     */
    public function saveQuoteAfter($evt){
        $quote = $evt->getQuote();
        if($quote->getSsn()){
            $var = $quote->getSsn();
            if(!empty($var)){
                $model = Mage::getModel('custom/custom_quote');
                $model->deteleByQuote($quote->getId(),'ssn');
                $model->setQuoteId($quote->getId());
                $model->setKey('ssn');
                $model->setValue($var);
                $model->save();
            }
        }
    }
    /**
     *
     * When load() function is called on the quote object,
     * we read our custom fields value from database and put them back in quote object.
     * @param unknown_type $evt
     */
    public function loadQuoteAfter($evt){
        $quote = $evt->getQuote();
        $model = Mage::getModel('custom/custom_quote');
        $data = $model->getByQuote($quote->getId());
        foreach($data as $key => $value){
            $quote->setData($key,$value);
        }
    }
    /**
     *
     * This function is called after order gets saved to database.
     * Here we transfer our custom fields from quote table to order table i.e sales_order_custom
     * @param $evt
     */
    public function saveOrderAfter($evt){
        $order = $evt->getOrder();
        $quote = $evt->getQuote();
        if($quote->getSsn()){
            $var = $quote->getSsn();
            if(!empty($var)){
                $model = Mage::getModel('custom/custom_order');
                $model->deleteByOrder($order->getId(),'ssn');
                $model->setOrderId($order->getId());
                $model->setKey('ssn');
                $model->setValue($var);
                $order->setSsn($var);
                $model->save();
            }
        }
    }
    /**
     *
     * This function is called when $order->load() is done.
     * Here we read our custom fields value from database and set it in order object.
     * @param unknown_type $evt
     */
    public function loadOrderAfter($evt){
        $order = $evt->getOrder();
        $model = Mage::getModel('custom/custom_order');
        $data = $model->getByOrder($order->getId());
        foreach($data as $key => $value){
            $order->setData($key,$value);
        }
    }

}

So, what we have done above is captured the quote events sales_quote_save_before,sales_quote_save_after and sales_quote_load_after.

Using these events saved our custom field to database. Details of functions are given in comments. We have already create model classes for this Stack_Custom_Model_Custom_Order Stack_Custom_Model_Custom_Quote

I hope you know which are required files for making the custom table to work in magento


Admin Order View Page So as per your need to show our custom field in Admin -> Order -> View Order page. To do this, in our adminhtml layout file, in my case custom.xml we will put in this code.

    <?xml version="1.0"?>
<layout version="0.1.0">
    <adminhtml_sales_order_view>
        <reference name="order_info">
            <action method='setTemplate'><template>custom/sales/order/info.phtml</template></action>
            <block type="adminhtml/sales_order_view_info" name="order_info2" template="sales/order/view/info.phtml"></block>
            <block type="custom/adminhtml_custom_order" name="custom.order" template='custom/order.phtml'/>
        </reference>
    </adminhtml_sales_order_view>
</layout>

So basically here i have added my block “custom.order” to the existing order_info block. So the code for block class custom/adminhtml_custom_order would be

<?php
class Stack_Custom_Block_Adminhtml_Custom_Order extends Mage_Adminhtml_Block_Sales_Order_Abstract{
    public function getCustomVars(){
        $model = Mage::getModel('custom/custom_order');
        return $model->getByOrder($this->getOrder()->getId());
    }
}

and next we need to create their phtml files custom/order.phtml

    <?php $_order = $this->getOrder() ?>
<div class="box-left">
    <div class="entry-edit">
        <div class="entry-edit-head">
            <h4 class="icon-head head-account"><?php echo Mage::helper('sales')->__('Custom Fields') ?></h4>
        </div>
        <div class="fieldset">
            <table cellspacing="0" class="form-list">
            <?php
                $custom = $this->getCustomVars();
                foreach($custom as $key => $value){
            ?>
            <tr>
                <td style="width:10%" class="label"><strong><?php echo Mage::helper('sales')->__($key) ?></strong></td>
                <td class="value"><?php echo $value;?></td>
            </tr>
            <?php } ?>
            </table>
        </div>
    </div>
</div>
<div class="clear"></div>

custom/sales/order/info.phtml

getChildHtml('order_info2');?> getChildHtml('custom.order');?>
2
  • ok thanks for the reply i can hide the shipping information.But the added ssn(or any other extra field) how can I save the value to database .and i need to show the value in admin section also
    – developer
    Mar 7, 2017 at 10:53
  • @sujana The above code is updated as per your requirement to save the custom field value into database IT will work please accept my answer Mar 7, 2017 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.