1

I want to display the coupon code used, if any, in the orders grid. The field should be filterable and sortable.

How can I achieve this ?

1

The below answer will show Ambiguous status column error when you filter collection using as "status" column is in both the tables, so to resolve this we have to add below line before $this->setCollection($collection);

$collection->addFilterToMap('status', 'main_table.status);

| improve this answer | |
0

Main class for order grid is "Mage_Adminhtml_Block_Sales_Order_Grid", if you want to add some column you have to rewrite this class like below :

config.xml

<blocks>
    <adminhtml>
        <rewrite>
            <sales_order_grid>Company_Module_Block_Adminhtml_Order_Grid</sales_order_grid>
        </rewrite>
    </adminhtml>
</blocks>

You need to join sales_flat_order and sales_flat_order_grid table in order to show coupon_code in sales order grid.

Grid.php

<?php
/**
* Sales Order Class
*/
class Company_Module_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{  
    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 Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
    }
    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();
    }
}
| improve this answer | |

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.