Today I am going to show you how to add column to magento order grid through our custom module either anyway. I mean either by overriding the order grid block or by observing the event. In my previous article add product attribute as column to category products section I have displayed how you can add some extra column to product grid. Now I am just showing you how you can simply add postcode and company as a extra column to order grid in magento. What I know its just a simple , Like all other grid section you have to do two things one is add column to by _prepareColumns methods and other is set collection query then your work is done.So for adding extra column to the those two method you will either override or use observer. I always choose the second option that is event observer.Because it is most reliable one to do .So here I will let you know both methods how you can do easily setp by step.

add extra column to magento order grid
add extra column to magento order grid

First method:

Event Observer:

If you have little bit idea on event observer concept in magento then you can easily understand what I am explaining here.so lets start with event observer way.

One best thing I would like tell you that how to identify which event is suitable for your requirement.This is the simple way I mean just go to mage.php though dir : app/mage.php and search for dispatchEvent function near to line No – 444 and do you log by Mage::log($name); or Mage::log($name,1,’name.log’,true) make sure log is enabled in magento configuration filed and see which event is suitable for your requirement.I am not explaining more on this topic because this is not our topic.Here our topic is different ,so lets come to our point.I have used here two event which is full-filling my requirement .You can do the same if like this concept.

lets come to the first file called config.xml file,which is located in our module by dir :
app/code/local/[NamespaceName]/[ModuleName]/etc/config.xml
e.g: /app/code/local/JRB/Ordercolumns/etc/config.xml

<config>
    <modules>
        <jrb_ordercolumns>
            <version>0.1.0</version>
        </jrb_ordercolumns>
    </modules>
    ----------------------------
    ----------------------------
    <events>
        <core_block_abstract_prepare_layout_before>
            <observers>
                <admingridaddcolumn>
                    <class>JRB_Ordercolumns_Model_Observer</class>
                    <method>addNewColumnToOrderGrid</method>
                </admingridaddcolumn>
            </observers>
        </core_block_abstract_prepare_layout_before>
        <sales_order_grid_collection_load_before>
            <observers>
                <admingridaddcolumn3>
                    <class>JRB_Ordercolumns_Model_Observer</class>
                    <method>beforeGridCollectionLoad</method>
                </admingridaddcolumn3>
            </observers>
        </sales_order_grid_collection_load_before>
    </events>
    --------------------------
    --------------------------
</config>

In this above code I have defined two methods through different event called as beforeGridCollectionLoad by the event sales_order_grid_collection_load_before and other is addNewColumnToOrderGrid by the event core_block_abstract_prepare_layout_before.

lets see what are the logic I have implementted in that observer file. I would like to tell you that logic is that I have just add the column by taking method _prepareColumns as a block object and add the column by addColumn method and fr the collection I also just have get collection object and added some extra query to that collection to get my requirement.Lets see what is that

<?php

public function addNewColumnToOrderGrid(Varien_Event_Observer $observer)
{
    $block = $observer->getEvent()->getBlock();
   
    if( ($block instanceof Mage_Adminhtml_Block_Sales_Order_Grid)  ) {
        
        $block->addColumnAfter('postcode',
            array(
                'header'=> Mage::helper('catalog')->__('Postcode'),
                'index' => 'postcode'
            ),
            'shipping_name'
        );
        
        $block->addColumnAfter('company',
            array(
                'header'=> Mage::helper('catalog')->__('Company'),
                'index' => 'company'
            ),
            'postcode'
        );
    }
}
?>

and other is

<?php
 public function beforeGridCollectionLoad(Varien_Event_Observer $observer){
    $collection = $observer->getOrderGridCollection();
    if (!isset($collection)) {
        return;
    }
    if ($collection instanceof Mage_Sales_Model_Resource_Order_Grid_Collection) {
       /* @var $collection Mage_Catalog_Model_Resource_Product_Collection */
       $collection->getSelect()->joinInner(
            array(
                'order_address' => 'sales_flat_order_address'
            ),
            'order_address.parent_id = main_table.entity_id'
        )->group(array('main_table.entity_id', 'order_address.parent_id'));
    }
}

?>

Second method:

Overriding Blocks:

Sometimes if you are not getting success on the above methods and you don’t have enough time to research then do this easiest methods and do your work as quick as possible. Lets see how we can do it by overriding blocks and implement on our custom module.

lets come to the first file called config.xml file same as above method,which is located in our module by dir :
app/code/local/[NamespaceName]/[ModuleName]/etc/config.xml
e.g: /app/code/local/JRB/Ordercolumns/etc/config.xml

 <config>
    <modules>
        <jrb_ordercolumns>
            <version>0.1.0</version>
        </jrb_ordercolumns>
    </modules>
    -----------------------
    -----------------------
    <global>
        <jrb_ordercolumns>
        <class>JRB_Ordercolumns_Block</class>
    </jrb_ordercolumns>
        <blocks>
            <adminhtml>
                <rewrite>
                    <sales_order_grid>JRB_Ordercolumns_Block_Adminhtml_Order_Grid</sales_order_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
    ------------------------
    -----------------------
</config>

In the above code I mean in config.xml file I have overrides the order_grid method to my custom order_grid method through rewrite tag.Lets what the logic I have placed in block grid file.

Here in the block file I just have add the column trough _prepareColumns and set the collection through setCollection method nothing else. All are the same logic for grid section.

<?php
/**
* JRB_Ordercolumns_Block_Adminhtml_Order_Grid
* 
* @category   JRB
* @package    JRB_Ordercolumns
* @author     Jyotiranjan Biswal [biswal@jyotiranjan.in]
*/
class JRB_Ordercolumns_Block_Adminhtml_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
    /**
    * @category   JRB
    * @package    JRB_Ordercolumns
    * @method     addNewColumnToOrderGrid
    * @author     Jyotiranjan Biswal [biswal@jyotiranjan.in]
    * 
    * @return     will set the collection with join query to the recent collection
    */
    public function setCollection($collection)
    {
        $collection->getSelect()->joinInner(
            array(
                'order_address' => 'sales_flat_order_address'
            ),
            'order_address.parent_id = main_table.entity_id'
        )->group(array('main_table.entity_id', 'order_address.parent_id'));
        
        parent::setCollection($collection);
    }
    /**
    * @category   JRB
    * @package    JRB_Ordercolumns
    * @method     addNewColumnToOrderGrid
    * @author     Jyotiranjan Biswal [biswal@jyotiranjan.in]
    * 
    * @return     will add column to ordergrid through recent block object
    */
    protected function _prepareColumns()
    {
        $this->addColumnAfter('postcode',
            array(
                'header'=> Mage::helper('catalog')->__('Postcode'),
                'index' => 'postcode'
            ),
            'shipping_name'
        );
        
        $this->addColumnAfter('company',
            array(
                'header'=> Mage::helper('catalog')->__('Company'),
                'index' => 'company'
            ),
            'postcode'
        );
        
        return parent::_prepareColumns();
    }
    
}

?>

Hope you enjoyed the article add column to magento order grid and never hesitate to share to help others also you can get the full module from here JRB_Ordercolumns

Thanks

add column to magento order grid
Tagged on:                                                 

Leave a Reply

Your email address will not be published. Required fields are marked *