If you want to add custom fields in order grid, use the following process to custom fields from product and customer grid: First create Block, Helper, controllers, etc folder in the following folder ‘app/code/community/Custom/Orders’.

Now we will add create the files step by step: Create the order.php file in the following folder and add the code below:

Folder Path:  app/code/community/Custom/Orders/Block/Adminhtml/Sales

<?php
 
class Custom_Orders_Block_Adminhtml_Sales_Order extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_blockGroup = 'custom_orders';
        $this->_controller = 'adminhtml_sales_order';
        $this->_headerText = Mage::helper('custom_orders')->__('<img src="'.Mage::getBaseURL('skin').'adminhtml/default/default/images/catalog/icon/fam_folder_table.gif" /> Orders');
		//$this->_addButtonLabel = 'Create New Orders';
		$this->_addButton('order_reorder', array(
              'label'     => Mage::helper('sales')->__('Create New Orders'),
              'onclick'   => "setLocation('".$this->getUrl('/sales_order_create/start')."')"

          )); 
 
        parent::__construct();
        $this->_removeButton('add'); 
    }
}

?>

Now create grid.php file in order folder and here you can add your custom column which you want to show on order grid:

<?php
 
class Custom_Orders_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('custom_order_grid');
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }
	
	
	protected function _getCollectionClass()
    {
        return 'sales/order_grid_collection';
    }
 
    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('sales/order_collection')
            ->join(array('a' => 'sales/order_address'), 'main_table.entity_id = a.parent_id AND a.address_type != \'billing\'', array(
                'city'       => 'city',
                'country_id' => 'country_id'
            ))
            ->join(array('c' => 'customer/customer_group'), 'main_table.customer_group_id = c.customer_group_id', array(
                'customer_group_code' => 'customer_group_code'
            ))
            ->addExpressionFieldToSelect(
                'fullname',
                'CONCAT({{customer_firstname}}, \' \', {{customer_lastname}})',
                array('customer_firstname' => 'main_table.customer_firstname', 'customer_lastname' => 'main_table.customer_lastname'))
            ->addExpressionFieldToSelect(
                'products',
                '(SELECT GROUP_CONCAT(\' \', x.name)
                    FROM sales_flat_order_item x
                    WHERE {{entity_id}} = x.order_id
                        AND x.product_type != \'configurable\')',
                array('entity_id' => 'main_table.entity_id')
            )
			->addExpressionFieldToSelect(
                'sku',
                '(SELECT GROUP_CONCAT(\' \', x.sku)
                    FROM sales_flat_order_item x
                    WHERE {{entity_id}} = x.order_id
                        AND x.product_type != \'configurable\')',
                array('entity_id' => 'main_table.entity_id')
            );
 
        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }
 
    protected function _prepareColumns()
    {
        $helper = Mage::helper('custom_orders');
        $currency = (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
 
        $this->addColumn('increment_id', array(
            'header' => $helper->__('Order #'),
            'index'  => 'increment_id'
        ));
 
        $this->addColumn('purchased_on', array(
            'header' => $helper->__('Purchased On'),
            'type'   => 'datetime',
            'index'  => 'created_at'
        ));
		
		$this->addColumn('fullname', array(
            'header'       => $helper->__('Bill To Name'),
            'index'        => 'fullname',
            'filter_index' => 'CONCAT(customer_firstname, \' \', customer_lastname)'
        ));
		
		
        $this->addColumn('products', array(
            'header'       => $helper->__('Products Name'),
            'index'        => 'products',
            'filter_index' => '(SELECT GROUP_CONCAT(\' \', x.name) FROM sales_flat_order_item x WHERE main_table.entity_id = x.order_id AND x.product_type != \'configurable\')'
        ));
		
			$this->addColumn('sku', array(
            'header'       => $helper->__('Product SKU'),
            'index'        => 'sku',
            'filter_index' => 'sku'
        ));
        
 /*
        $this->addColumn('city', array(
            'header' => $helper->__('City'),
            'index'  => 'city'
        ));
 
        $this->addColumn('country', array(
            'header'   => $helper->__('Country'),
            'index'    => 'country_id',
            'renderer' => 'adminhtml/widget_grid_column_renderer_country'
        ));
 */
  $this->addColumn('customer_email', array(
        'header' => $this->helper('sales')->__('Customer Email'),
        'index' => 'customer_email',
    ));
	
        $this->addColumn('customer_group', array(
            'header' => $helper->__('Customer Group'),
            'index'  => 'customer_group_code'
        ));
 
        $this->addColumn('grand_total', array(
            'header'        => $helper->__('Grand Total'),
            'index'         => 'grand_total',
            'type'          => 'currency',
            'currency_code' => $currency
        ));
 
        $this->addColumn('shipping_method', array(
            'header' => $helper->__('Shipping Method'),
            'index'  => 'shipping_description'
        ));
 
        $this->addColumn('order_status', array(
            'header'  => $helper->__('Status'),
            'index'   => 'status',
            'type'    => 'options',
            'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
        ));
		
		
		 if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
            $this->addColumn('action',
                array(
                    'header'    => Mage::helper('sales')->__('Action'),
                    'width'     => '50px',
                    'type'      => 'action',
                    'getter'     => 'getId',
                    'actions'   => array(
                        array(
                            'caption' => Mage::helper('sales')->__('View'),
                            'url'     => array('base'=>'*/sales_order/view'),
                            'field'   => 'order_id',
                            'data-column' => 'action',
                        )
                    ),
                    'filter'    => false,
                    'sortable'  => false,
                    'index'     => 'stores',
                    'is_system' => true,
            ));
        }
		
 
        $this->addExportType('*/*/exportCustomCsv', $helper->__('CSV'));
        $this->addExportType('*/*/exportCustomExcel', $helper->__('Excel XML'));
 
        return parent::_prepareColumns();
    }
	
	
	   protected function _prepareMassaction()
    {
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('order_ids');
        $this->getMassactionBlock()->setUseSelectAll(false);

        if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/cancel')) {
            $this->getMassactionBlock()->addItem('cancel_order', array(
                 'label'=> Mage::helper('sales')->__('Cancel'),
                 'url'  => $this->getUrl('*/sales_order/massCancel'),
            ));
        }

        if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/hold')) {
            $this->getMassactionBlock()->addItem('hold_order', array(
                 'label'=> Mage::helper('sales')->__('Hold'),
                 'url'  => $this->getUrl('*/sales_order/massHold'),
            ));
        }

        if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/unhold')) {
            $this->getMassactionBlock()->addItem('unhold_order', array(
                 'label'=> Mage::helper('sales')->__('Unhold'),
                 'url'  => $this->getUrl('*/sales_order/massUnhold'),
            ));
        }

        $this->getMassactionBlock()->addItem('pdfinvoices_order', array(
             'label'=> Mage::helper('sales')->__('Print Invoices'),
             'url'  => $this->getUrl('*/sales_order/pdfinvoices'),
        ));

        $this->getMassactionBlock()->addItem('pdfshipments_order', array(
             'label'=> Mage::helper('sales')->__('Print Packingslips'),
             'url'  => $this->getUrl('*/sales_order/pdfshipments'),
        ));

        $this->getMassactionBlock()->addItem('pdfcreditmemos_order', array(
             'label'=> Mage::helper('sales')->__('Print Credit Memos'),
             'url'  => $this->getUrl('*/sales_order/pdfcreditmemos'),
        ));

        $this->getMassactionBlock()->addItem('pdfdocs_order', array(
             'label'=> Mage::helper('sales')->__('Print All'),
             'url'  => $this->getUrl('*/sales_order/pdfdocs'),
        ));

        $this->getMassactionBlock()->addItem('print_shipping_label', array(
             'label'=> Mage::helper('sales')->__('Print Shipping Labels'),
             'url'  => $this->getUrl('*/sales_order_shipment/massPrintShippingLabel'),
        ));

        return $this;
    }
	
 
    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }
}

?>

Create OrderController.php file in the folder ‘app/code/community/Custom/Orders/controllers’ and add below code:

<?php
 
class Custom_Orders_Adminhtml_OrderController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_title($this->__('Sales'))->_title($this->__('Orders'));
        $this->loadLayout();
        $this->_setActiveMenu('sales/sales');
        $this->_addContent($this->getLayout()->createBlock('custom_orders/adminhtml_sales_order'));
        $this->renderLayout();
    }
 
    public function gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('custom_orders/adminhtml_sales_order_grid')->toHtml()
        );
    }
 
    public function exportCustomCsvAction()
    {
        $fileName = 'orders_custom.csv';
        $grid = $this->getLayout()->createBlock('custom_orders/adminhtml_sales_order_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
    }
 
    public function exportCustomExcelAction()
    {
        $fileName = 'orders_custom.xml';
        $grid = $this->getLayout()->createBlock('custom_orders/adminhtml_sales_order_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
    }
}

Now create Adminhtml folder with ordercontroller.php file and add below code:

<?php
 
class Custom_Orders_Adminhtml_OrderController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_title($this->__('Sales'))->_title($this->__('Orders'));
        $this->loadLayout();
        $this->_setActiveMenu('sales/sales');
        $this->_addContent($this->getLayout()->createBlock('custom_orders/adminhtml_sales_order'));
        $this->renderLayout();
    }
	
	public function startAction()
{
    Mage::getSingleton('adminhtml/session_quote')->clear();
    $this->_redirect('*/sales_order_create', array(
        'customer_id' => $this->getRequest()->getParam('customer_id'),
        'store_id'    => $this->getRequest()->getParam('store_id')
    ));
}
 
    public function gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('custom_orders/adminhtml_sales_order_grid')->toHtml()
        );
    }
 
    public function exportCustomCsvAction()
    {
        $fileName = 'orders_custom.csv';
        $grid = $this->getLayout()->createBlock('custom_orders/adminhtml_sales_order_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
    }
 
    public function exportCustomExcelAction()
    {
        $fileName = 'orders_custom.xml';
        $grid = $this->getLayout()->createBlock('custom_orders/adminhtml_sales_order_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
    }
}
?>

Create config.xml in etc folder with the following code:

<?xml version="1.0"?>
 
<config>

  <menu>
        <sales>
            <children>
                <sales_orders>
                    <disabled>1</disabled>
                </sales_orders>
            </children>
        </sales>
    </menu>

    <modules>
        <Custom_Orders>
            <version>1.1.1.1</version>
        </Custom_Orders>
    </modules>
 
    <global>
        <models>
            <custom_orders>
                <class>Custom_Orders_Model</class>
                <resourceModel>custom_orders_resource</resourceModel>
            </custom_orders>
        </models>
 
        <resources>
            <custom_orders_setup>
                <setup>
                    <module>Custom_Orders</module>
                </setup>
            </custom_orders_setup>
        </resources>
 
        <blocks>
		  <adminhtml>
            <rewrite>
                <sales_order>Custom_Orders_Block</sales_order>
            </rewrite>
        </adminhtml>

           <!-- <adminhtml>
                <rewrite>
                    <catalog_category_tab_product>Custom_Orders_Block_Adminhtml_Catalog_Category_Tab_Product</catalog_category_tab_product>
					<catalog_product_grid>Custom_Orders_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
                </rewrite>
				
            </adminhtml>-->
      
		
            <custom_orders>
                <class>Custom_Orders_Block</class>
            </custom_orders>
        </blocks>
 
        <helpers>
            <custom_orders>
                <class>Custom_Orders_Helper</class>
            </custom_orders>
        </helpers>
    </global>
 
   <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <custom_orders before="Mage_Adminhtml">Custom_Orders_Adminhtml</custom_orders>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Now create adminhtml.xml in etc folder:

<?xml version="1.0"?>
 
<config>
    <menu>
        <sales>
            <children>
                <custom_orders translate="title" module="custom_orders">
                    <sort_order>10</sort_order>
                    <title>Orders</title>
                    <action>adminhtml/order/</action>
                </custom_orders>
				<order>
				  <depends>
                                <module>HideMe</module> 
                   </depends>
				</order>
				
            </children>
        </sales>
    </menu>
</config>

Now create blank helper class in helper folder:

<?php
class Custom_Orders_Helper_Data extends Mage_Core_Helper_Abstract
{
 
}

Now create extension enable/disable file in etc/module folder with the name Custom_Orders.xml:

<?xml version="1.0"?>
 
<config>
    <modules>
        <Custom_Orders>
            <active>true</active>
            <codePool>community</codePool>
        </Custom_Orders>
    </modules>
</config>

Now you can see your added column add in order grid.

 

Related Article

0 thoughts on “Enhanced Order Grid for Magento 1 – Magento Tutorial”

Leave a Reply