Magento – How to Add a Total Row in a Grid

adminhtmlgridgrid-serlizationmagento-1.7

I'm creating a new module that uses a order grid for particular product(this is not the report grid) in magento admin i successfully create new order grid for that but i want to get total price for product at the end , like in sales>report.

Please see image

Image

Any help will be appreciated,,Thanks

Best Answer

1.Sipmle way, add these fields to your gird class, see mine:

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
     */

}

My Result enter image description here

2.Using Magento Reports: (But this one is so complicated)
You should create report grid. See Mage_Adminhtml_Block_Report_Customer_Orders_Grid report grid and try to impelement own. Here is great tuts about custom reports:
http://codegento.com/2011/03/creating-custom-magento-reports
http://www.summasolutions.net/blogposts/custom-reports-magento

Related Topic