Magento Add new Column under sales item order

By | May 17, 2014

Recently I faced issues how to  add column to some table before it gets save in Magento.so here is the steps which will help to add the new column under sales_flat_order_item, sales_flat_invoice_item and sales_flat_shipment_item

so my field name is “vendor_name” in each of 3 tables and update the information without firing any query!
Step 1)
Create the installer script which will add the column in all 3 tables

<?php
$installer = $this;

$installer->startSetup();

$installer->run("
ALTER TABLE sales_flat_order_item ADD COLUMN vendor_name varchar(255) NULL;
ALTER TABLE sales_flat_invoice_item ADD COLUMN vendor_name varchar(255) NULL;
ALTER TABLE sales_flat_shipment_item ADD COLUMN vendor_name varchar(255) NULL;
");

$installer->endSetup();

?>

So once you run the script column will get added in your tables

STEP 2

so now we need to go for the event which will fire before order is placed, before invoice is created and before shipment is saved.add the events in config.xml

<sales_order_place_before>
<observers>
<adding_vendor_name_order>
<type>singleton</type>
<class>Namespace_Module_Model_Observer</class>
<method>saveVendorNameInOrder</method>
</adding_vendor_name_order>
</observers>
</sales_order_place_before>

<sales_order_invoice_save_before>
<observers>
<adding_vendor_name_invoice>
<type>singleton</type>
<class>Namespace_Module_Model_Observer</class>
<method>saveVendorNameInInvoice</method>
</adding_vendor_name_invoice>
</observers>
</sales_order_invoice_save_before>

<sales_order_shipment_save_before>
<observers>
<adding_vendor_name_shipment>
<type>singleton</type>
<class>Namespace_Module_Model_Observer</class>
<method>saveVendorNameInShipment</method>
</adding_vendor_name_shipment>
</observers>
</sales_order_shipment_save_before>

Step 3)

Now we need to create the observer.php

<?php
public function saveVendorNameInOrder(Varien_Event_Observer $observer) {
$order = $observer->getEvent()->getOrder();
/* add your bussiness Logic here */
return $this;
}

public function saveVendorNameInInvoice(Varien_Event_Observer $observer) {
$invoice = $observer->getEvent()->getInvoice();
/* add your bussiness Logic here */
return $this;
}

public function saveVendorNameInShipment(Varien_Event_Observer $observer)
{
$shipment = $observer->getEvent()->getShipment();
/* add your bussiness Logic here */
return $this;
}

?>

so once all done Clear the cache and the code will work as per need..hope this will help you

Leave a Reply