How to improve search results within Magento Manage Products Grid

Store administration can be a rather difficult task when one has to deal with a big amount of information. In Magento it is always a products catalog and both orders and clients lists. How to find a required product with several thousands or tens of thousands products? And how to find a client among tens of thousands records?

In such case one can use search. Magento administrative panel allows usage of comprehensive search through all the elements (Global Record Search), i.e. in one search field one can look for products, orders and clients.

It is often necessary to find a product according to its name when site administrator doesn't exactly remember it.

For example:

We need to find a product with the name "A5 Inkjet Greeting Card Paper Gloss Super 260g With Envelope Pack"

If we start searching for "A5 Inkjet Greeting Card Paper" we will get the result immediately. But if we do not remember the product's exact name and start searching for "A5 Inkjet Gloss 260g" we won't get any result at all because Magento cannot perform search with random word order.

The same problem emerges when products are filtered in Manage Products Grid according to their names. Name search with random word order will not bring any results.

Below is our solution for the problem of search in Magento administrative panel. The solution will substantially ease a site administrator's difficulties as it allows to find the required information quickly even when random word order is used in search.

A patch to filter products in Manage Products Grid is made.

Local file rewrite is created

/app/code/local/Mage/Adminhtml/Block/Widget/Grid.php

The following part of code in the function protected function _addColumnFilterToCollection($column)

        if ($this->getCollection()) {
            $field = ( $column->getFilterIndex() ) ? $column->getFilterIndex() : $column->getIndex();
            if ($column->getFilterConditionCallback()) {
                call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
            } else {
                $cond = $column->getFilter()->getCondition();
                if ($field && isset($cond)) {
                    $this->getCollection()->addFieldToFilter($field , $cond);
                }
            }
        }
        return $this;

is changed into the following code:

        if ($this->getCollection()) {
            $field = ( $column->getFilterIndex() ) ? $column->getFilterIndex() : $column->getIndex();
            if ($column->getFilterConditionCallback()) {
                call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
            } else {
                /*$cond = $column->getFilter()->getCondition();
                if ($field && isset($cond)) {
                    $this->getCollection()->addFieldToFilter($field , $cond);,
                }*/
                
                //start of patch
                $cond = $column->getFilter()->getCondition();
                if ($field && isset($cond)) {
                    if (isset($cond['like'])){
                        $x = $cond['like'];
                        $termsArr = preg_split('/\s+/',(string)$x);
                        foreach ($termsArr as $term){
                            $term = preg_replace('/%/','',$term);
                            $term = preg_replace("/'/",'',$term);
                            $term = "'%$term%'";
                            $newZendDbExpr = new Zend_Db_Expr($term);
                            $modifCond = array('like'=>$newZendDbExpr);
                            $this->getCollection()->addFieldToFilter($field , $modifCond);
                        }
                        
                    }else{
                        $this->getCollection()->addFieldToFilter($field , $cond);
                    }
                }
                //end of patch
            }
        }
        return $this;

In our next post we will continue to dwell in the problem of search in Magento administrative panel and will consider one of the options for the solution for product search in Global Records Search using random key word order in the search request

 

Stay tuned. Comment and share this article.