0

This question already has an answer here:

I have created order report grid using custom module but the problem is i can't get total of custom field in the grid bottom.

enter image description here Grid.php

    public function __construct()
    {
        parent::__construct();
        $this->setId('sales_order_grid');
        $this->setUseAjax(true);
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);

    }

    /**
     * Retrieve collection class
     *
     * @return string
     */
    protected function _getCollectionClass()
    {
        return 'sales/order_grid_collection';
    }
    public function getReport($from, $to) {
        if ($from == '') {
            $from = $this->getFilter('report_from');
        }
        if ($to == '') {
            $to = $this->getFilter('report_to');
        }
        $totalObj = Mage::getModel('reports/totals');
        $totals = $totalObj->countTotals($this, $from, $to);
        $this->setTotals($totals);
        $this->addGrandTotals($totals);
        return $this->getCollection()->getReport($from, $to);
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }



    /* display total row at the bottom of grid */
    protected $_countTotals = true;
       public function getTotals()
        {
            $totals = new Varien_Object();
            $fields = array(
                'base_grand_total' => 0
             //actual column index, see _prepareColumns()
            );
            foreach ($this->getCollection() as $item) {
                foreach($fields as $field=>$value){
                    $fields[$field]+=$item->getData($field);
                }
            }
            //First column in the grid
            $fields['entity_id'] = 'Totals';
            $totals->setData($fields);
            //print_r($totals->toArray());exit;
            return $totals;
        }
    /* end total row code */
    protected function _prepareColumns()
    {


        $this->addColumn('seriolno', array(
            'header'=> Mage::helper('sales')->__('No #'),
            'width' => '80px',
            'type'  => 'text',
            'index' => 'entity_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')->__('Travel Date'),
            'index' => 'created_at',
            'type' => 'datetime',
            'width' => '100px',
        ));

        $this->addColumn('billing_name', array(
            'header' => Mage::helper('sales')->__('Customer Name'),
            'index' => 'billing_name',
        ));
        $this->addColumnAfter('adult', array(
                'header' => Mage::helper('sales')->__('Adult'),
                'filter'    => false,
                'index'     =>'adult',
                'type'      =>'number',
                'total' => 'sum',   
                'sortable'  => false,            
                'is_system' => false,
                'renderer'  => new DW_OrderProfit_Block_Adminhtml_Sales_Order_Renderer_Adult() //for the value
            ), 'billing_name');  
            $this->addColumnAfter('child', array(
                'header' => Mage::helper('sales')->__('Child'),
                'filter'    => false,
                'index'     =>'child',
                'sortable'  => false,            
                'is_system' => false,
                'renderer'  => new DW_OrderProfit_Block_Adminhtml_Sales_Order_Renderer_Child() //for the value
            ), 'adult');  
            $this->addColumnAfter('totaladultchild', array(
                'header' => Mage::helper('sales')->__('Total'),
                'filter'    => false,
                'sortable'  => false,
                'index'     =>'totaladultchild',            
                'renderer'  => new DW_OrderProfit_Block_Adminhtml_Sales_Order_Renderer_Totaladultchild() //for the value
            ), 'child');

        $this->addColumn('base_grand_total', array(
            'header' => Mage::helper('sales')->__('Grand Totals'),
            'index' => 'base_grand_total',
            'type'  => 'currency',
            'currency' => 'base_currency_code',
        ));


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

        return parent::_prepareColumns();
    }



public function getRowUrl($row)
  {
      //return $this->getUrl('*/*/edit', array('id' => $row->getId()));
      return false;
  }

    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }

any help would be much appreciate.Thanks

marked as duplicate by Marius, David Manners, Keyul Shah, Foo Bar Apr 3 '14 at 9:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    I don't know if this can help you, do not try to "find" options like in the core reports or something, why dont you just have a new "grid.phtml" where you can manually add a total row at the bottom and you already have "block" where you can access your data and just sum it up and show them there. I think doing that way you will save a lots of time! – Subesh Pokhrel Mar 4 '14 at 6:08
1

A similar question has been posted before, please search for similar questions before posting one.

How to add a Total row in a magento grid

The answer below:

class SSD_Uzkart_Block_Adminhtml_Uzkart_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
protected $_countTotals = true;

public function getTotals()
{
    $totals = new Varien_Object();
    $fields = array(
        'uzkart_trans_amount' => 0, //actual column index, see _prepareColumns()
        'some_summarable_field' => 0,
        'another_countable_field' => 0,
    );
    foreach ($this->getCollection() as $item) {
        foreach($fields as $field=>$value){
            $fields[$field]+=$item->getData($field);
        }
    }
    //First column in the grid
    $fields['entity_id']='Totals';
    $totals->setData($fields);
    return $totals;
}

protected function _prepareColumns()
{
    /**
     * another columns
     */

    $this->addColumn('uzkart_trans_amount', array(
        'header' => Mage::helper('uzkart')->__('Payment Amount'),
        'index' => 'uzkart_trans_amount',
        'type' => 'currency',
    ));

    /**
     * another columns
     */
}

/**
 * another methods
 */

}

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