4

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

7

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

I don't think that rewriting the Grid Block is the best solution as it is not needed. Magento has an easy solution to add data to the grid table, this way you don't need to rewrite the grid and add a join with the sales_flat_order table.

In a previous post I explained the process with a complete example, see Add Column to a grid (observer) - Column ‘store_id’ in where clause is ambiguous issue for details on how to add this field to the grid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.