Magento – Add SKU and Product Name to Sales Order Grid

magento-enterpriseorder-gridsales

I am trying to add "sku" and "product name" to sales order grid with filtering.

I have created my joins but the columns are empty and I'm not sure what I have done incorrectly here…

Trying to get collection from sales/flat_order_item

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());

    $collection->getSelect()->joinLeft(array('sfoi' => 'sales_flat_order_item'),
        'sfoi.parent_id=main_table.entity_id', array('sfoi.sku',
        'sfoi.name'));

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

And add to the grid

protected function _prepareColumns()
{
    $this->addColumn('sku', array(
        'header'=> Mage::helper('sales')->__('Sku'),
        'width' => '80px',
        'type'  => 'text',
        'filter_index' => 'sfoi.sku',
    ));

    $this->addColumn('name', array(
        'header'=> Mage::helper('sales')->__('Product Name'),
        'width' => '80px',
        'type'  => 'text',
        'filter_index' => 'sfoi.name',
    ));
}

Can anyone see what I have done wrong here please..?

Best Answer

protected function _prepareColumns()
{
    $this->addColumn('sku', array(
        'header'=> Mage::helper('sales')->__('Sku'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'sfoi.sku',
        'filter_index' => 'sfoi.sku',
    ));

    $this->addColumn('name', array(
        'header'=> Mage::helper('sales')->__('Product Name'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'sfoi.name',
         'filter_index' => 'sfoi.name',
    ));
}

you haven't assign index to column