Sometimes there's a requirement for some new attributes for orders in a Magento store. The reasons may be different, extend functionality of Magento categories or simply to store more information about your online customers. This enables you to customize the checkout page in your store and make customers happier.

If you are a developer, then you must know adding custom product attribute for orders is pretty a common task. If you are facing problem with this task, we will be very much glad when you find this information helpful.

 Here are some quick steps and tips to create new attributes for orders in Magento store which will do exactly what you want and nothing more than that. So let’s start with it:

 For Adding new attribute, we have to create new column for new attribute field in “sales_flat_order” and “sales_flat_quote”.

 This can be done by two ways.

Firstly by creating custom module using Module Mysql setup file (file path- /app/code/local/softprodigy/customattribute/sql/customattribute_setup/mysql4-install-1.1.php) 

Here (Softprodigy -NameSpace, customattribute-Module)

$installer = $this;
$installer->startSetup();
$installer->addAttribute(“order”, “delivery_date”, array(“type”=>”varchar”));
$installer->addAttribute(“quote”, “
delivery_date“, array(“type”=>”varchar”));
$installer->endSetup();

Secondly, if you don't want to create your own module simply create a php file in the project’s root folder with the below code and run this file manually.

<?php require_once(‘app/Mage.php’);
Mage::app()->setCurrentStore(Mage::getModel(‘core/store’)->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$installer = new Mage_Sales_Model_Mysql4_Setup;
$attribute  = array(
‘type’  => ‘varchar’,
‘backend_type’  => ‘varchar’,
‘frontend_input’ => ‘varchar’,
‘is_user_defined’ => true,
‘label’  => ‘Delivery Date,
‘visible’ => true,
‘required’  => false,
‘user_defined’  => false,
‘searchable’  => false,
‘filterable’  => false,
‘comparable’  => false,
‘default’  => ”
);
$installer->addAttribute(‘order’, ‘order_note‘, $attribute);
$installer->addAttribute(‘quote’, ‘order_note‘, $attribute);
$installer->endSetup();

echo “success”;

Run this file on root folder

 Now we have created a space for our custom attribute in DB(Created new column in sales_flat_order and sales_flat_quote table)

 After this, we have to add this custom attribute to global scope in config.xml.

 (If you have been using custom module file path is-/app/code/local/Softprodigy/customattribute/etc/config.xml

if not,/app/code/core/mage/sales/etc/config.xml. But editing core file isn’t good practice)


            <global>
             …..
              <fieldsets>
                 <sales_convert_quote>
                        <order_note><to_order>*</to_order></order_note>
                                    </sales_convert_quote>

                           <sales_convert_order>
 <order_note><to_quote>*</to_quote></order_note>
 </sales_convert_order>
 </fieldsets>
 …

</global>

Then create an event in this same config.xml file

<global>
….
<events>
<checkout_type_onepage_save_order>
<observers>
<order_note_observer>
<type>singleton</type>
<class>customattribute/observer</class>
<method>saveCustomData</method>
</order_note_observer>
</observers>
</checkout_type_onepage_save_order>
</events>
…..
</global>

<class>customattribute/observer</class> –Edit as per your custom module name (in core file you can simply put sales/observer)

 The above event triggers the following observer method

public function saveCustomData($observer) {
 $event = $observer->getEvent();
 $order = $event->getOrder();
$fieldVal = Mage::app()->getFrontController()->getRequest()->getParams();
 $order->setDeliveryDate($fieldVal[‘delivery_date‘]);
}

Now we are in final step. In last, add your custom attribute frontend script in checkout page

<input id=”order_note” name=”order_note” value=”” class=”input-text” type=”text”>

 Then finally get our custom order attribute value in backend by editing following file with add following code-app/design/adminhtml/default/default/template/sales/order/view/info.phtml

<?php if($_order->getOrderNote()): ?>
<tr>
<td class=”label”><label><?php echo Mage::helper(‘sales’)->__(‘Order Note’) ?></label></td>
<td class=”value”><strong><?php echo $_order->getOrderNotee(); ?></strong></td>
 </tr>
 <?php endif; ?>