Magento – Show SKU and Product Name in Sales_Order_Grid

adminhtmlgrid-serlizationmagento-1.8order-grid

I have been stuck for hours trying to add both Product Name and SKU to the Sales Order Grid in Magento.

I have created the joins and added the columns but the no data is being displayed and can't figure out why???

Any help would be nice…

Inside _prepareCollection

protected function _prepareCollection()
{
          $collection->getSelect()->joinLeft(array('sfoi' => 'sales_flat_order_item'),
            'order_id=main_table.entity_id = quote_item_id', array('sfoi.sku',
            'sfoi.name'));
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

And my two columns

$this->addColumn('sku', array(
    'header' => Mage::helper('sales')->__('SKU'),
    'align' => 'left',
    'sortable' => true,
    'index' => 'sku'
));

$this->addColumn('name', array(
    'header' => Mage::helper('sales')->__('Item Name'),
    'align' => 'left',
    'sortable' => true,
    'index' => 'name'
));

$this->addColumn('total_item_count', array(
    'header' => Mage::helper('sales')->__('Total Items'),
    'width' => '80px',
    'index' => 'total_item_count',
    'type' => 'text',
    'filter_index' => 'total_item_count'
));

I am able to get number of items into the Grid but not name and SKU is just empty

Could do with a heads up here!

Best Answer

Rather then put complex query you can just use the Renderer in Magento Grid

 $this->addColumn('protect_code', array(
        'header' => Mage::helper('sales')->__('Bought Games'),           
        'index' => 'protect_code',
        'type'  => 'text',      
        'width' => '250px',
        'sortable'  =>false,
        'filter' => false,                                         
        'renderer' => 'NameSpace_ModuleName_Block_Adminhtml_Renderer_Productssku',          
    ));

Then you have to create the Renderer File NameSpace\ModuleName\Block\Adminhtml\Renderer\Productssku.php

Write down below code in you this file

<?php
class NameSpace_ModuleName_Block_Adminhtml_Renderer_Productssku extends     Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {       
        //$getData = $row->getData();               
        $order=Mage::getModel('sales/order')->load($row['entity_id']);              
        $str="";
        $i=1;
        foreach($order->getAllItems() as $_order){                      
            $str.=$_order->getSku();
            if($i!=count($order->getAllItems()))
                $str.=", ";
                $i++;
    }       
        unset($order);
        return $str;
    }
}
Related Topic