Add a row for displaying total balance at the end of the grid Magento Admin GiftCardAccount

1.7k Views Asked by At

In the magento admin control panel,

Customers = > GiftCardAccounts

I want to display the total of Balance as a row just below the grid . I tried to set $this->setCountTotals(true); in the method public function __construct() in the Grid.php, but it didn't worked. Please let me know how to do this exactly. Any help would be greatly appreciated.

1

There are 1 best solutions below

3
On

I came across yours as I was searching for an answer to this for myself. I find it interesting that this seems to be a non-existant topic of discussion online - maybe everyone has it already figured out?

Here is what you will need to do:

In etc/config.xml of your module, override the block like so:

<blocks>
    <enterprise_giftcardaccount>
        <rewrite>
            <adminhtml_giftcardaccount_grid>Namespace_Giftcardaccount_Adminhtml_Giftcardaccount_Grid</adminhtml_giftcardaccount_grid> <!-- I like to put overrides/rewrites in their same folder under my namespace -->
        </rewrite>
    </enterprise_giftcardaccount>
</blocks>

Now, in Namespace/Giftcardaccount/Block/Adminhtml/Giftcardaccount/Grid.php, do this:

<?php

class Namespace_Giftcardaccount_Adminhtml_Giftcardaccount_Grid extends Enterprise_GiftCardAccount_Block_Adminhtml_Giftcardaccount_Grid {

    protected function _prepareGrid()
    {
        $collection = $this->getCollection();

        $balanceTotal = 0;
        foreach ($collection as $giftCardAccount) {
            $balanceTotal += $giftCardAccount->getBalance();
        }

        $this->setTotals(new Varien_Object(
            array(
                'balance' => $balanceTotal
            )
        );

        $this->setCountTotals(true);

        return parent::_prepareGrid();
    }

}

That should do it!