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());
        $this->setCollection($collection);    
        //start     
        $filter = $this->getParam('filter');
    	$filter_data = Mage::helper('adminhtml')->prepareFilterString($filter);		             
    	echo '<pre>';
    	print_r($filter_data);
    	echo '</pre>';
        //end 
        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)

Array
(
    [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 thoughts on “Grid filters in Magento

  1. 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

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

Scroll to top