View and Filter Product sku and name in Magento sales order grid

This feature is really very important, but by default this feature is missing. So lets do it.
NameSpace : MyPackage and Module: MyModule

Module configuration

<!-- app/etc/modules/MyPackage_MyModule.xml -->
<config>
    <modules>
        <MyPackage_MyModule>
            <active>true</active>
            <codePool>local</codePool>
        </MyPackage_MyModule>
    </modules>
</config>

Create config file for this module

<!-- app/code/local/MyPackage/MyModule/etc/config.xml -->
<?xml version="1.0"?>
<config>
    <modules>
        <MyPackage_MyModule>
            <version>0.0.0.1</version>
        </MyPackage_MyModule>
    </modules>
    <global>
        <blocks>
            <mymodule>
                <class>MyPackage_MyModule_Block</class>
            </mymodule>
            <adminhtml>
                <rewrite>
                    <sales_order_grid>MyPackage_MyModule_Block_Adminhtml_Sales_Order_Grid</sales_order_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

Create a Grid.php file in the following location.

<?php
// app/code/local/MyPackage/MyModule/Block/Adminhtml/Sales/Order/Grid.php
class MyPackage_MyModule_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    
}

Copy all content from /app/code/core/Mage/Adminhtml/Block/Sales/Order/grid.php into app/code/local/MyPackage/MyModule/Block/Adminhtml/Sales/Order/Grid.php.

<?php
// app/code/local/MyPackage/MyModule/Block/Adminhtml/Sales/Order/Grid.php
class MyPackage_MyModule_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('sales_order_grid');
        $this->setUseAjax(true);
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
    }

    /**
     * Retrieve collection class
     *
     * @return string
     */
    protected function _getCollectionClass()
    {
        return 'sales/order_grid_collection';
    }
---------------
--------
----------
    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }
}

Now replace _prepareCollection() function to the following code,

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass())
            ->join(
                'sales/order_item',
                '`sales/order_item`.order_id=`main_table`.entity_id',
                array(
                    'sku'  => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ",")'),
                    'name' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ",")'),
                )
            );
        $collection->getSelect()->group('entity_id');
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

After adding this function lets add the columns in grid for sku and product name inside _prepareColumns() function.

$this->addColumn('sku', array(
            'header'    => Mage::helper('Sales')->__('Skus'),
            'width'     => '100px',
            'index'     => 'sku',
            'type'        => 'text',
        ));

        $this->addColumn('name', array(
            'header'    => Mage::helper('Sales')->__('Product Names'),
            'width'     => '100px',
            'index'     => 'name',
            'type'        => 'text',
        ));

Enjoy!

3 thoughts on “View and Filter Product sku and name in Magento sales order grid

  1. Working well except when i search for virtual product it throws an error– No date part in ‘NULL’ found in app/code/core/Zend/Date.php(1091): Zend_Date->_calculate(‘set’, ‘NULL’, ‘yyyy-MM-dd HH:m…’, ‘en_US’)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s