For Adding custom attribute, we have to create new column for custom attribute field in “sales_flat_order” and “sales_flat_quote”
We can create it By 2 ways
1.By custom Module Mysql setup file (file path-/app/code/local/pwt/singlepagecheckout/sql/singlepagecheckout_setup/mysql4-install-1.1.php)
Here (pwt -NameSpace, singlepagecheckout-Module)
$installer = $this;
$installer->startSetup();
$installer->addAttribute(“order”, “delivery_date”, array(“type”=>”varchar”));
$installer->addAttribute(“quote”, “delivery_date“, array(“type”=>”varchar”));
$installer->endSetup();
2.If you don’t have a 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’, ‘delivery_date‘, $attribute);
$installer->addAttribute(‘quote’, ‘delivery_date‘, $attribute);
$installer->endSetup();echo “success”;
Run this file on root folder
Now we have allotted 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/pwt/singlepagecheckout/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>
<delivery_date><to_order>*</to_order></delivery_date>
</sales_convert_quote><sales_convert_order>
<delivery_date><to_quote>*</to_quote></delivery_date>
</sales_convert_order>
</fieldsets>
…
</global>
Then create an event in this same config.xml file
<global>
….
<events>
<checkout_type_onepage_save_order>
<observers>
<delivery_date_observer>
<type>singleton</type>
<class>singlepagecheckout/observer</class>
<method>saveCustomData</method>
</delivery_date_observer>
</observers>
</checkout_type_onepage_save_order>
</events>
…..
</global>
<class>singlepagecheckout/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=”delivery_date” name=”delivery_date” value=”” class=”input-text” type=”text”>
EDIT: We can create only one custom observer function. If you want to create more than one attribute, you can use that same observer method for saving those multiple custom attributes.
(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->getDeliveryDate()): ?>
<tr>
<td class=”label”><label><?php echo Mage::helper(‘sales’)->__(‘Delivery Date’) ?></label></td>
<td class=”value”><strong><?php echo $_order->getDeliveryDate(); ?></strong></td>
</tr>
<?php endif; ?>

No comments:
Post a Comment