PHP Magento Tips

Adding New Column to Orders Grid in Magento Admin with Custom Module

Rate this posting:
{[['']]}

In the previous articles (Orers Grid, Customers Grid) you might find the way how to add custom column/attribute to adminhtml (orders, customers, ect) grid. But this is another example. In our article we will use the layout handles for inserting columns to the orders grid.

Step 1: add below code in  app/etc/modules/Mycompany_CustomOrderGrid.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Mycompany_CustomOrderGrid>
            <active>true</active>
            <codePool>local</codePool>
        </Mycompany_CustomOrderGrid>
    </modules>
</config>

Step 2: add below code in  app/code/local/Mycompany/CustomOrderGrid/etc/config.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Mycompany_CustomOrderGrid>
            <version>0.1.0</version>
        </Mycompany_CustomOrderGrid>
    </modules>
    <global>
        <models>
            <mycompany_customordergrid>
                <class>Mycompany_CustomOrderGrid_Model</class>
            </mycompany_customordergrid>
        </models>
    </global>
    <adminhtml>
        <events>
            <sales_order_grid_collection_load_before>
                <observers>
                    <mycompany_customordergrid>
                        <model>mycompany_customordergrid/observer</model>
                        <method>salesOrderGridCollectionLoadBefore</method>
                    </mycompany_customordergrid>
                </observers>
            </sales_order_grid_collection_load_before>
        </events>
        <layout>
         <updates>
             <mycompany_customordergrid>
                 <file>customordergrid/extendedgrid.xml</file>
             </mycompany_customordergrid>
         </updates>
     </layout>
    </adminhtml>
</config>

Step 3: In  app/code/local/Mycompany/CustomOrderGrid/Model/Observer.php add below code:

<?php 
class Mycompany_CustomOrderGrid_Model_Observer extends Varien_Event_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'));
  
 }
}
?>

Step 4: add below code in  app/design/adminhtml/default/default/layout/customordergrid/extendedgrid.xml
<?xml version="1.0"?>
<layout>
    <sales_order_grid_update_handle>
        <reference name="sales_order.grid">
            <action method="addColumnAfter">
                <columnId>payment_method</columnId>
                <arguments>
                    <header>Payment Method</header>
                    <index>payment_method</index>
                    <filter_index>payment.method</filter_index>
                    <type>text</type>
                </arguments>
                <after>shipping_name</after>
            </action>
        </reference>
    </sales_order_grid_update_handle>
    <adminhtml_sales_order_grid>
        <!-- apply layout handle defined above -->
        <update handle="sales_order_grid_update_handle" />
    </adminhtml_sales_order_grid>
    <adminhtml_sales_order_index>
        <!-- apply layout handle defined above -->
        <update handle="sales_order_grid_update_handle" />
    </adminhtml_sales_order_index>
</layout>
Tag : Magento
0 Comments On "Adding New Column to Orders Grid in Magento Admin with Custom Module"

Back To Top