Add a total row in admin grid Magento

Magento has possibility to add total row in any grid in admin panel. You just need to use some additional methods in your Grid Block.

In your Grid Block that extended Mage_Adminhtml_Block_Widget_Grid you need to enable row with totals:

protected $_countTotals = true;

and implement method for calculation totals

    
    public function getTotals()
    {
        $totals = new Varien_Object();
        $fields = array(
            'entity_id' => $this->__('Total'), //first column in the grid
            'order_number' => 0, //real names of columns, @see method _prepareColumns
            'order_amount' => 0,

        );
        foreach ($this->getCollection() as $item) {
            foreach($fields as $field=>$value){
                $fields[$field]+=$item->getData($field);
            }
        }
        $totals->setData($fields);
        return $totals;
    }

You will see something like this

totals

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.