Friday, September 21, 2012

MAGENTO::Sales Order Grid Customization - Add SKU list and customer email column to the grid


Add SKU list and Customer Email column to the grid

Supposed you had copied your code\core\Mage\Adminhtml\Block\Sales\Order\Grid.php to code\local\Mage\Adminhtml\Block\Sales\Order\Grid.php because you want to display the subtotals, credit card types, total of items ordered for the sales order. The new version of Magento which is 1.4.1

in code\local\Mage\Adminhtml\Block\Sales\Order\Grid.php change like this

protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft( array('sfoi'=>'sales_flat_order_item'), 'main_table.entity_id=sfoi.order_id', array( 'sku' =>'sfoi.sku' ) );
$collection->addExpressionFieldToSelect ( "sku", "GROUP_CONCAT(sku SEPARATOR ' , ')", $fields="" );
$collection->getSelect()->group("entity_id");
$collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'), 'main_table.entity_id=sfo.entity_id', array('customer_email'));
$this->setCollection($collection);
$collection->addFilterToMap('created_at', 'main_table.created_at');
return parent::_prepareCollection();
}

protected function _prepareColumns(){ 
......
......
$this->addColumn('sku', array(
            'header'=> Mage::helper('catalog')->__('SKU'),
            'width' => '140px',
            'type'  => 'text',
            'index' => 'sku',
        ));
        $this->addColumn('customer_email', array(
            'header' => Mage::helper('sales')->__('Customer Eamil'),
            'index' => 'customer_email',
    'width' => '140px',
        ));
........
........
}

in app\code\local\Mage\Sales\Model\Mysql4\Order\Collection.php
public function getSelectCountSql()
{
/* @var $countSelect Varien_Db_Select */
$countSelect = parent::getSelectCountSql();

$countSelect->resetJoinLeft();
$countSelect->reset(Zend_Db_Select::GROUP); // add this to reset the GROUP
return $countSelect;
}