1

Hello Guys I am not Getting How to add discount field In Magento Admin grid.

Also Please explain how to add custom table value Display In order Grid.

1

Yup Got the Solutions For this we have to rewrite the class

Mage_Adminhtml_Block_Sales_Order_Grid

1) Create you custom module in magento and first create your Module configuration file in app/etc/module/Keyul_Discountadd.xml folder.

<?xml version="1.0"?>
<config>
  <modules>
    <Keyul_Discountadd>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Keyul_Discountadd>
  </modules>
</config>

2) Create the folders in app/code/local/Keyul/Discountadd/Block and app/code/local/Keyul/Discountadd/etc respectively.

3) create the config.xml file in etc folder as below.

<?xml version="1.0"?>
<config>
  <modules>
    <Keyul_Discountadd>
      <version>0.1.0</version>
    </Keyul_Discountadd>
  </modules>
  <global>    
    <blocks>
      <discountadd>
        <class>Keyul_Discountadd_Block</class>
      </discountadd>
            <adminhtml>
                <rewrite>
                        <sales_order_grid>Keyul_Discountadd_Block_Adminhtml_Sales_Order_Grid</sales_order_grid>
            </rewrite>
        </adminhtml>
    </blocks>
  </global>
</config> 

4) create the app\code\local\Keyul\Discountadd\Block\Adminhtml\Sales\Order\grid.php file as below.

Add Below code

<?php
class Keyul_Discountadd_Block_Adminhtml_Sales_Order_Grid extends     Mage_Adminhtml_Block_Sales_Order_Grid
{
 protected function _prepareCollection()
    {
    //$collection = Mage::getResourceModel($this->_getCollectionClass());               
    $collection = Mage::getResourceModel('sales/order_collection')
        ->addAttributeToSelect('*');        
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _prepareColumns()
{

    $this->addColumn('real_order_id', array(
        'header'=> Mage::helper('sales')->__('Order #'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'increment_id',
    ));

    if (!Mage::app()->isSingleStoreMode()) {
        $this->addColumn('store_id', array(
            'header'    => Mage::helper('sales')->__('Purchased From (Store)'),
            'index'     => 'store_id',
            'type'      => 'store',
            'store_view'=> true,
            'display_deleted' => true,
        ));
    }

    $this->addColumn('created_at', array(
        'header' => Mage::helper('sales')->__('Purchased On'),
        'index' => 'created_at',
        'type' => 'datetime',
        'width' => '100px',
    ));

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

    $this->addColumn('shipping_name', array(
        'header' => Mage::helper('sales')->__('Ship to Name'),
        'index' => 'shipping_name',
    ));
     $this->addColumn('discount_amount', array(
        'header' => Mage::helper('sales')->__('discount_amount'),
        'index' => 'discount_amount',
    ));

    $this->addColumn('base_grand_total', array(
        'header' => Mage::helper('sales')->__('G.T. (Base)'),
        'index' => 'base_grand_total',
        'type'  => 'currency',
        'currency' => 'base_currency_code',
    ));

    $this->addColumn('grand_total', array(
        'header' => Mage::helper('sales')->__('G.T. (Purchased)'),
        'index' => 'grand_total',
        'type'  => 'currency',
        'currency' => 'order_currency_code',
    ));

    $this->addColumn('status', array(
        'header' => Mage::helper('sales')->__('Status'),
        'index' => 'status',
        'type'  => 'options',
        'width' => '70px',
        'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
    ));

    if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
        $this->addColumn('action',
            array(
                'header'    => Mage::helper('sales')->__('Action'),
                'width'     => '50px',
                'type'      => 'action',
                'getter'     => 'getId',
                'actions'   => array(
                    array(
                        'caption' => Mage::helper('sales')->__('View'),
                        'url'     => array('base'=>'*/sales_order/view'),
                        'field'   => 'order_id'
                    )
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
                'is_system' => true,
        ));
    }
    $this->addRssList('rss/order/new', Mage::helper('sales')->__('New Order RSS'));

    $this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV'));
    $this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel XML'));

    return $this;
    }
}

5) Flush you cache and open sales grid it will appear Discount in grid.

Note. Don't Change the Directly in core files If you want to change then rewrite the class and then do changes.

  • Discount Field is come on Order Grid but discount amount is not showing. What should we do to show discount amount? – Rafiqul Islam Nov 7 '18 at 12:55
0

Here I'm again with a solution to add columns to the Sales Grid without using a rewrite, since some time Magento uses a special sales_flat_order_grid table to build the Sales Order grid page. The benefit for this is that you don't need to add joins or rewrite core files in order to display extra columns. In Add Column to a grid (observer) - Column ‘store_id’ in where clause is ambiguous issue I made a complete sample extension on how you can add an extra column. In the example change the column name and your done.

The observer used by Magento for this takes care of the update for the records when order data changes.

  • @viadimir this is also very good example as well Thanks :) – Keyul Shah Jan 14 '14 at 12:34

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.