This issue you can solve by customizing the renderer file. You can fetch the format from database by custom query as well.
For eg.
Prodid.php
<?php
class Mage_Adminhtml_Block_Sales_Order_Renderer_Prodid extendsMage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row){
$url = $this->getUrl('*/*/edit', array(
'store'=>$this->getRequest()->getParam('store'),
'id'=>$row->getId())
);
$exploding = explode('sales_order/edit/id/',$url);
//print_r($exploding);
$id = explode('/',$exploding[1]);
//print_r($id);
$myId = $id[0];
//echo $myId;
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
// now $write is an instance of Zend_Db_Adapter_Abstract
$readresult=$write->query("SELECT product_id FROM `sales_flat_order_item` WHERE `order_id` =$myId ");
while ($row = $readresult->fetch() ) {
$productid = $row['product_id'];
$myprodid .= $productid.',';
}
$myprodid2 = substr($myprodid,0,-1);
return $myprodid2;
}
}
?>
This will give you the desired result in my case it was product id
Then calling it in the grid file would be like
$this->addColumn('ids', array(
'header' => Mage::helper('sales')->__('Product ID'),
'width' => '100px',
'index' => 'ids',
'type' => 'text',
'filter' => false,
'sortable' => false,
'renderer' => 'Mage_Adminhtml_Block_Sales_Order_Renderer_Prodid'
));
The filter=false and sortable false will remove the a href from the headers and will only show you the data.
If you want to make them sortable, then you will need to pass the corresponding unique index id.
Please share if anything is unclear here.
As Magento save coupon code
on Sales_flat_Order table so you need to MySQL join with Sales_flat_order
table
Add sales_flat_order table to collection.In magento, sales_flat_order and sales_flat_order_grid table related with Flat Grid order table entity_id and sales_flat_order table entity_id
.
$select = $collection->getSelect();
$select->joinLeft(array('order' => Mage::getModel('core/resource')->getTableName('sales/order')), 'order.entity_id=main_table.entity_id',
array('coupon_code' => 'coupon_code'))
Rewrite class then add columns to show Coupon:
<?php
/**
* Sales Order Class
*
* @author Amit Bera
*/
class Amit_CustomOrderGrid_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
protected function _prepareColumns()
{
$this->addColumn('coupon_code', array(
'header' => Mage::helper('sales')->__('Color Name'),
'align' =>'left',
'width' => '50px',
'index' => 'coupon_code',
'filter_index' => 'order.coupon_code'
));
parent::_prepareColumns();
return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();
}
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(array('order' => Mage::getModel('core/resource')->getTableName('sales/order')),
'order.entity_id=main_table.entity_id',array('coupon_code' => 'coupon_code'))
$this->setCollection($collection);
// return parent::_prepareCollection();
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
}
?>
Best Answer
The
sales_flat_order_grid
table is updated at every order save action. You can add custom columns to the table in your own extension, if you add a column name that is already used in the sales_flat_order table you do not need to add anything extra, on every order-save action the columns are updated (if needed). If you want to insert data from a different table you will need to create an observer for thesales_order_resource_init_virtual_grid_columns
event to collect and prepare the join.For more details and a working example see my answer in Add Column to a grid (observer) - Column ‘store_id’ in where clause is ambiguous issue