We all know how to add column to Magento sales order grid but here we found the best way to add additional columns to Magento sales order grid with no or little damage caused to Magento core classes -:
In this article, we are going to add customer email address to the sales order grid
Step 1 – Extend Sales Order Collection using by using sales_order_grid_collection_load_before event
<events> <sales_order_grid_collection_load_before> <observers> <scommerce_custom_sale_order_grid_collection_load_before> <class>Scommerce_Custom_Model_Observer</class> <method>addEmailToOrderCollection</method> </scommerce_custom_sale_order_grid_collection_load_before> </observers> </sales_order_grid_collection_load_before> </events>
Step 2 – Add customer email address to the order grid collection using sales_order_grid_collection_load_before event declared in Step 1
public function addEmailToOrderCollection(Varien_Event_Observer $observer) { if ($this->h()->isEnabled() && $this->isAdmin()) { $collection = $observer->getEvent()->getOrderGridCollection(); $collection->getSelect() ->join(array('order' => $collection->getTable('sales/order')), 'order.entity_id=main_table.entity_id', array('customer_email' => 'customer_email')); return $collection; } }
Step 3 – Now display this column to the grid using core_layout_block_create_after event
<core_layout_block_create_after> <observers> <scommerce_custom_add_new_grid_columns> <class>Scommerce_Custom_Model_Observer</class> <method>addNewGridColumns</method> </scommerce_custom_add_new_grid_columns> </observers> </core_layout_block_create_after>
Step 4 – Add customer email address column in the sales order grid using core_layout_block_create_after event declared in Step 3
public function addNewGridColumns(Varien_Event_Observer $observer) { $block = $observer->getBlock(); if ($block instanceof Mage_Adminhtml_Block_Sales_Order_Grid) { $block->addColumnAfter('customer_email', array( 'header' => Mage::helper('adminhtml')->__('Customer Email'), 'index' => 'customer_email' ), 'increment_id' ); } }
The above two events will work to get you the new column in sales order grid but there is a problem because now your getting data from sales_order_grid and sales_flat_order tables and both these tables have common fields between them for example created_at, updated_at, increment_id, store_id etc. If you start applying filter on sales order grid then you will see ambiguous column error straight away.
To fix the ambiguous column error in sales order grid, Magento has addFilterToMap function and this function will rescue us from this problem but unfortunately in this case you have to extend one of the Magento core class Mage_Sales_Model_Resource_Order_Grid_Collection which takes us to our next step
Step 5 – Declare in config.xml to extend Mage_Sales_Model_Resource_Order_Grid_Collection class
<models> <sales_resource> <rewrite> <order_grid_collection>Scommerce_Custom_Model_Resource_Sales_Order_Grid_Collection</order_grid_collection> </rewrite> </sales_resource> </models>
Step 6 – Now map filters of sales order grid columns using the magic function addFilterToMap
class Scommerce_Custom_Model_Resource_Sales_Order_Grid_Collection extends Mage_Sales_Model_Resource_Order_Grid_Collection { /** * Init select * @return Mage_Core_Model_Resource_Db_Collection_Abstract */ protected function _initSelect() { $this->addFilterToMap('store_id', 'main_table.store_id') ->addFilterToMap('created_at', 'main_table.created_at') ->addFilterToMap('updated_at', 'main_table.updated_at') ->addFilterToMap('increment_id', 'main_table.increment_id'); return parent::_initSelect(); } }
This will map the common fields between two tables sales_order_grid and sales_flat_order. This will sort out the ambiguous column issue. That’s it!
Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.