Home » Magento9 February 2011

Magento: How to search or filter by multiselect attribute in admin grid?

Suppose you have a multi select attribute and you have displayed it in admin grid. You have displayed the multi select attribute options as selection list. Now, you want to filter/search the grid by the multiselect attribute.

The problem here is that the multiselect attribute value is store as comma separated value in database. When we send single value from the selection list, the filter doesn’t work properly.

The solution is to use filter_condition_callback in addColumn.

Here is the code:-

$this->addColumn('categories',
                array(
                    'header'=> Mage::helper('mymodule')->__('Categories'),
                    'index' => 'categories',
                    'width' => '150px',
                    'type' => 'options',
                    'options' => $categories,
                    'filter_condition_callback'
                                => array($this, '_filterCategoriesCondition'),
            ));

The callback function is:-

protected function _filterCategoriesCondition($collection, $column)
{
	if (!$value = $column->getFilter()->getValue()) {
		return;
	}

	$this->getCollection()->addFieldToFilter('categories', array('finset' => $value));
}

The similar thing is done in Mage_Adminhtml_Block_Cms_Block_Grid class to filter Store View.

Hope this helps. Thanks.

From Mukesh Chapagain's Blog, post Magento: How to search or filter by multiselect attribute in admin grid?

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS Feed
  • Grewals

    Hey, maybe somebody knows how to display in product grid labels of options for multiselect attribute instead of ids?

  • Rait

    Hey Mukesh,

    nice blog you have here. Finding it extremely useful. Keep up the good work!

    In the meantime, maybe you can help me on the right tracks with my problem. I am trying to filter the products that are displayed on the “Assigned Products” tab of the “Configurable Product” edit page.

    The actual filtering is not the issue, but finding the method that’s responsible for retrieving that list, is. At first I thought it was the _prepareCollection() method from the Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Super/Config/Grid.php file. Howver, it turns ot this is not the case.

  • Felixgmca

    how will search in catalog_product page in magento

  • max

    Wonderful! Thanks for this blog Mukesh Chapagain!

  • Ricky

    Cool snippet, thank you Mukesh, I’m using it on manage product grid, but cats dropdown is empty, what I have to do to filter by categories the pruduct list?

  • Andreas

    Works perfectly, thank you!

  • sandeep verma

    For frontend Layer Search : – I need to find query text in attribute color,size,category please suggets

    Ex:- like user enter “Black 36″ (search result should show all the combination with Black and size 36 using AND not with OR )

  • Magento Developer

    Alternatively you can also use: _addColumnFilterToCollection() method