Grid filters in Magento

Since last few days I am working on a custom admin module, and was required to make a functionality related to Filters.

The functionality was such that I needed to fetch the filters inserted in the Grid, that is if we are searching records with names like ‘john’, so here ‘john’ will be filter.

Now it was bit confusing here, Magento encodes (base64_encode) the filter inserted, so I was not able to find the filters in session directly, but after studying the code and wasting lot of time was able to find the solution at last.

I am writting here the code for fetching the filters, I am taking Orders Grid (Adminend > Sales > Orders) for example.

Please open file app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php

Now write the below code in function _prepareCollection

    protected function _prepareCollection()
        $collection = Mage::getResourceModel($this->_getCollectionClass());
        $filter = $this->getParam('filter');
    	$filter_data = Mage::helper('adminhtml')->prepareFilterString($filter);		             
    	echo '<pre>';
    	echo '</pre>';
        return parent::_prepareCollection();                

Now search records with ‘Bill to Name’ like ‘john’ (or as per your records), and you will be able to get array as shown here (as per your search terms)

    [created_at] => Array
            [locale] => en_US

    [billing_name] => john

Note: I am using the default Magento code for the above example, it is recomended to override this grid in local

Hope this helps.

6 Responses to “Grid filters in Magento”

  1. dev says:

    Nice thanks man…It was very helpful for me :)

  2. Sanjeewanie says:

    This was very helpful for me.Thank you very much

  3. richard says:

    you also can read the description and learn user guide of that extension.
    maybe it will be helpful for you

  4. Mariano says:

    Exactly what I was looking for. Thanks a lot man!

  5. rajiv says:

    Nicely presented. It was exactly what I needed.
    I just enable the module when I need to filter, but any ideas what might be causing this? We’re on 1.5.1 and don’t have any other extensions installed that affect the product grid.

Leave a Reply