Magento – magento Add Coupon code at sales order grid

gridmagento-1.7order-grid

I want to add coupon code in sales order grid . As we know Sales order gird build from sales_flat_order_grid table

Coupon_code column is exist in sales_flat_order table not sales_flat_order_grid table.

When i add coupon_code at sales order grid but when I search the coupon code with status column then got this error : Column 'status' in where clause is ambiguous

Note : I am working on existing project not new project

Magento version : 1.7.0.2

Can you please provide me solution

Best Answer

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();
    }
}
?>