Magento – How to set filter_index for a column which is not in the database table

collection;columnfiltergridmagento-1.7

My code is as follows. Everything works fine except the filteration of new column which shows abandoned cart item names comma separately.

I have overridden the block Mage_Adminhtml_Block_Report_Shopcart_Abandoned_Grid.

protected function _prepareCollection()
    {
$collection = Mage::getResourceModel('reports/quote_collection');

        $filter = $this->getParam($this->getVarNameFilter(), array());
        if ($filter) {
            $filter = base64_decode($filter);
            parse_str(urldecode($filter), $data);
        }


        if (!empty($data)) {
            $collection->prepareForAbandonedReport($this->_storeIds, $data);
        } else {
            $collection->prepareForAbandonedReport($this->_storeIds);
        }

        $this->setCollection($collection);
        Mage_Adminhtml_Block_Report_Grid_Shopcart::_prepareCollection();
//------------------ my modifications---------------
// This was done to show item names comma separately in the abandoned cart grid as a new column.
foreach($collection as $col)
       {
           $itemcollection = $col->getItemsCollection();
           $products = array();
           foreach ($itemcollection as $item)
           {
               $products[] = $item->getName();
           }
             $productname = implode( " , ", $products);
             $col->setQuoteItemName($productname); 
       }
//------------------ my modifications---------------

   return $this;
}

protected function _prepareColumns()
    {
        parent::_prepareColumns();

    $this->addColumnAfter('quote_item_name', array(
            'header'    =>Mage::helper('reports_abandonedcart')->__('Item(s) Name'),
            'index'     =>'quote_item_name',
            'filter_index'=>'sales_flat_quote.quote_item_name', // What should be here 
            'sortable'  =>false
        ),'items_qty');


        return Mage_Adminhtml_Block_Report_Grid_Shopcart::_prepareColumns();
}

Best Answer

I am not 100% sure if this will work but what you could do is add a filter_condition_callback to the column.

'filter_condition_callback' => array($this, 'yourFilterCallback'),

Then write your function yourFilterCallback

protected function yourFilterCallback($collection, $column)
{
    $filterValue = $column->getFilter()->getValue();
    // Do what ever you need to the collection here
    return $this;
}