Question

i am try to add columns in Admin > sales >order by overriding method. Copy app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php to app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php.

add this code in protected function _prepareCollection()

$collection->getSelect()->join('sales_flat_order_item', '`sales_flat_order_item`.order_id=`main_table`.entity_id', array('name'  => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.name SEPARATOR ", ")')));

add this code in protected function _prepareColumns()

$this->addColumn('name', array(
        'header' => Mage::helper('sales')->__('Product Name'),
        'index' => 'name',
    ));

but it get all get all product's name of all orders like in the image enter image description here

Was it helpful?

Solution

Take look at Show SKU and Product Name in Sales_Order_Grid?

You can do this one of two ways :

1) Using "renderer"

$this->addColumn('name', array(
    ....                                        
    'renderer' => 'NameSpace_ModuleName_Block_Adminhtml_Renderer_Productsname',          
));


class NameSpace_ModuleName_Block_Adminhtml_Renderer_Productsname extends     Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {                    
        $order=Mage::getModel('sales/order')->load($row->getData('entity_id'));              
        $str="";

        foreach($order->getAllVisibleItems() as $_item){                      
            $str .= $_item->getSku();
    }       
        unset($order);
        return $str;
    }
}

2) using "join" see https://magento.stackexchange.com/a/14343/519

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top