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

6
  • I don't quite understand what are you trying to achieve, but as a good starting point you can look at Mage_Adminhtml_Block_Report_Product_Grid::_afterLoadCollection(). On line 58 (Magento 1.7.0.2 CE) this method instantiates and populates Mage_Reports_Model_Totals(). Take a look at this class. Also check this Magestore article about total models : blog.magestore.com/2012/09/07/… Commented Jan 31, 2014 at 14:04
  • Hello @Keyur shah have you done with this?can you please answerTHIS how can i add total at the end of grid?
    – Zahirabbas
    Commented Feb 17, 2014 at 7:08
  • @Freedom i am achieving this . you can add total using overriding sales/grid.php and add below code by mazeUz
    – Keyur Shah
    Commented Feb 17, 2014 at 7:26
  • Thanks got it.but it does not display 'Totals' title as seen on the below image instead it display checkbox.Require any changes in the code?
    – Zahirabbas
    Commented Feb 17, 2014 at 7:38
  • @Freedom can you please check my answer ?
    – Keyur Shah
    Commented Feb 17, 2014 at 7:52

2 Answers 2

11

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

4
  • thanks for your answer it help me a lot +1 for it @mageUz
    – Keyur Shah
    Commented Feb 1, 2014 at 5:12
  • boom Finally it works flawlessly big thank you @mageUz
    – Keyur Shah
    Commented Feb 1, 2014 at 6:52
  • Hello I have used this code but cannot get the word TOTAL described when the grid is exported to CSV . Commented Aug 20, 2015 at 13:16
  • Hello, This worked for me but I am not able to get total of columns which are using Renderer.
    – Yogita
    Commented Aug 2, 2017 at 9:39
7

mageUz's answer is really good and simple.

I had some problems with the "action" column showing in totals row so I looked into it a bit further. So here it is...

1. Hide the Action Column in totals and more

Due to probably a bug in widget/grid.phtml the 'is_system' parameter is not taken under consideration, so it shows the action link in the totals row. In order to solve it, just add 'totals_label' => '', in your action column declaration.

The 'totals_label' => 'label', parameter can be used in all columns and it will override the totals cell even if it's set.

That said you can omit the $fields['entity_id']='Totals'; line of mageUz's answer, go to the 'entity_id' column (or any column you choose) and add

'totals_label' => $this->__('Total'), (+multilingual support)

2. Same result, slightly different approach

In your grid block add a protected function _prepareTotals($columns). Then call it in the _prepareCollection() function with the rows you want as totals comma separated. To be more clear your Whatever/Grid.php should look something like this

protected function _prepareCollection(){
  $collection = Mage::getResourceModel('mymodule/mymodel_collection');
  $this->setCollection($collection);
  $this->_prepareTotals('price,special_price'); //Add this Line with all the columns you want to have in totals bar
  return parent::_prepareCollection();
}

//Add following function
protected function _prepareTotals($columns = null){
  $columns=explode(',',$columns);
  if(!$columns){
    return;
  }
  $this->_countTotals = true;   
  $totals = new Varien_Object();
  $fields = array();
  foreach($columns as $column){
    $fields[$column]    = 0;    
  } 
  foreach ($this->getCollection() as $item) {
    foreach($fields as $field=>$value){
      $fields[$field]+=$item->getData($field);
    }
  }
  $totals->setData($fields);
  $this->setTotals($totals);
  return;
}

protected function _prepareColumns(){
  $this->addColumn('entity_id', array(
    'index'             => 'entity_id',
    'header'            => $this->__('ID'),
    'totals_label'      => $this->__('Total'), //Add this line to show "Total" in the beginning of the row
  ));

  $this->addColumn('name', array(
    'index'             => 'name',
    'header'            => $this->__('Name'),
  ));

  $this->addColumn('price', array(
    'index'             => 'price',
    'header'            => $this->__('Price'),
    'type'              => 'currency',
    'currency_code'     => $this->_getStore()->getBaseCurrency()->getCode(),
  ));

  $this->addColumn('special_price', array(
    'index'             => 'special_price',
    'header'            => $this->__('Special Price'),
    'type'              => 'currency',
    'currency_code'     => $this->_getStore()->getBaseCurrency()->getCode(),
  ));

  $this->addColumn('action',array(
    'header'        => $this->__('Action'),
    'width'         => '100',
    'type'          => 'action',
    'getter'        => 'getId',
    'actions'       => array(array(
                          'caption' => $this->__('edit'),
                          'url'     => array('base' => '*/*/edit'),
                          'field'       => 'product_id',
                      )),
    'filter'        => false,
    'sortable'      => false,
    'is_system'     => true,
    'totals_label'      => '' //Add this line to stop the action showing 
  ));
  return parent::_prepareColumns();
}
1
  • Using this method it seems that the total from exported CSV file doesn't contain the right data. What should be in total row is from first row from grid.
    – Denisa
    Commented Mar 10, 2015 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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