Extend Magento sales order grid using event observer

You can add new columns to the Sales Order grid easily by extending the block class: Mage_Adminhtml_Block_Sales_Order_Grid
But this is not the preferred way due to conflict issues.

Here I will show you how to extend order grid by using events(core_block_abstract_to_html_before & sales_order_grid_collection_load_before) only.

For example purpose, we will be adding Payment Method to sales order grid and MagePsycho_Gridextend will be the skeleton extension.
1. Register the events:


...
<adminhtml>
    <events>
        <core_block_abstract_to_html_before>
            <observers>
                <magepsycho_gridextend_core_block_abstract_to_html_before>
                    <class>magepsycho_gridextend/observer</class>
                    <method>coreBlockAbstractToHtmlBefore</method>
                </magepsycho_gridextend_core_block_abstract_to_html_before>
            </observers>
        </core_block_abstract_to_html_before>
        <sales_order_grid_collection_load_before>
            <observers>
                <magepsycho_gridextend_sales_order_grid_collection_load_before>
                    <class>magepsycho_gridextend/observer</class>
                    <method>salesOrderGridCollectionLoadBefore</method>
                </magepsycho_gridextend_sales_order_grid_collection_load_before>
            </observers>
        </sales_order_grid_collection_load_before>
    </events>
</adminhtml>
...

2. Create the observer model:


<?php
/**
 * @category   MagePsycho
 * @package    MagePsycho_Gridextend
 * @author     [email protected]
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
class MagePsycho_Gridextend_Model_Observer {

    /**
     * Moved to Block class
     * @param Varien_Event_Observer $observer
     */
    public function coreBlockAbstractToHtmlBefore(Varien_Event_Observer $observer)
    {
        /** @var $block Mage_Core_Block_Abstract */
        $block = $observer->getEvent()->getBlock();
        if ($block->getId() == 'sales_order_grid') {
            
            //add new column: payment method
            $paymentArray = Mage::getSingleton('payment/config')->getActiveMethods();
            $paymentMethods = array();
            foreach ($paymentArray as $code => $payment) {
                // not sure why ops_dl was not in the loop so tweaked it
                $paymentTitle = Mage::getStoreConfig('payment/'.$code.'/title');
                $paymentMethods[$code] = $paymentTitle;
            }
            $block->addColumnAfter(
                'payment_method',
                array(
                    'header'   => Mage::helper('sales')->__('Payment Method'),
                    'align'    => 'left',
                    'type'     => 'options',
                    'options'  => $paymentMethods,
                    'index'    => 'payment_method',
                    'filter_index'    => 'payment.method',
                ),
                'shipping_name'
            );
            
            //similary you can addd new columns
            //...

            // Set the new columns order.. otherwise our column would be the last one
            $block->sortColumnsByOrder();
        }
    }

    /**
     * Moved to block class
     * @param Varien_Event_Observer $observer
     */
    public function salesOrderGridCollectionLoadBefore(Varien_Event_Observer $observer)
    {
        $collection = $observer->getOrderGridCollection();
        $select = $collection->getSelect();
        $select->joinLeft(array('payment' => $collection->getTable('sales/order_payment')), 'payment.parent_id=main_table.entity_id',array('payment_method' => 'method'));
    }
}

3. Refresh the Sales > Order page, you will see a page similar to:
Sales Order Grid

Similarly, you can extend any admin grid-like sales invoice, customer, etc.