How to add custom fields in checkout page in magento

How to add custom fields in checkout page in magento.

In this blog, we will see how to add custom fields to magento onepage checkout steps. This method can be used to add custom field to any step in the checkout page, although in the current blog we have demonstrated how to add it to the billing step. This method will work on all magento versions.

This tutorial is for people who have a good knowledge of magento module development. Let start step by step, on how to add a single field on the billing step.

Step1: Adding HTML This is the most simple step.

We 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

and just above the line add this

So, as you can see we are simply adding a text with name as custom[ssn] and value as $this->getQuote()->getSsn()
After doing this, you should see a field in billing.phtml as shown Checkout Page Billing Step. You can add this field on any checkout step, it doesn’t matter for my code.

Step2: 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

Here we are creating two tables, one for quote and other for order. We will explain the use of order table later. 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 modules config.xml file and inside the tag put in

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

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


which you will find in the source code.

Detailed Explanation

The first event captured by our module is sales_quote_save_before and saveQuoteBefore() function is called in our Observer class. What this function does, just before $quote->save() function is called, it reads our custom variable value ‘ssn’ from FORM POST data and simply sets it in the $quote object.
The next event captured by our module is sales_quote_save_after and saveQuoteAfter() function is called in our Observer class. What this function does, just after $quote->save() function is called i.e the quote object get stored in database, it store our custom variable to our database table “sales_quote_custom”
The next event is sales_quote_load_after and function called is loadQuoteAfter(). When the $quote->load($id) is called, i.e quote is created from database entries, our function is called and this read the values of the custom variable from our database table “sales_quote_custom” and sets it back again in the quote object.
The next event captured in sales_model_service_quote_submit_after and function called is saveOrderAfter(). This event is called, just after an order is successfully placed. In this event, we read our custom field value from quote object and put in order table.
The next event is sales_order_load_after and function called is loadOrderAfter(). This event is fired when $order->load() function is called, we read our fields values from database and place it back in the order object.

Step3: Display Our Field in My Account -> View Order

After the order is placed by the customer, our custom fields needs to be visible in My Account. So to do this in our module fronted layout file, in my case its custom.xml.

This layout file is simple and understandable. Basically, we are taking reference of my.account.wrapper block and adding a child block to it. Our block type is
custom/order_view and template file is custom/order.phtml. So we need to create our block class

and our order.phtml file

After doing this, you should be able to see your custom field in My Account page as shown here My Account View Order

Step4: Admin Order View Page

Next we 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.

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

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


you can get this file from the source code, there code in file is the default magento file. After doing these steps in admin we should be able to see this Admin Order View Page

Step5: Adding Custom Fields to Order Email

The final step, is adding the custom fields selected by user to the order email. So when user places an order, we will add our custom field to the order email. To do this open the order email template located at
add the below code where you want

So as per the above code, we need to two functions hasCustomFields() and getFieldHtml() in our Order class. So next we will override the Mage_Sales_Model_Order model. Open your config.xml file and inside section put in this code

and next create our new model class Icorethink_Custom_Model_Sales_Order

This should add the custom fields to the order.

Thats it, we are done. Hopefully we have covered all sections related to order. If we missed something please comment below and we will add it.