Better Catalog Product Search in Magento

Written by Zak on November 4, 2014

The Problem

Here at Big Eye Deers we’re always looking for ways to get the most out of Magento and make it as user friendly as possible for our clients.

During a recent meeting we were discussing improvements that could be made to the backend of Magento to make it easier for store administrators to find the products they need to edit. We found the biggest problem was the catalog product search functionality within the catalog grid.  We’ve had several clients express frustration when trying to search by product name as it only searches for the literal string, rather than searching for any of the words given in the string.

For example, if our catalog contained these products:

  • Black Jacket Small
  • Black Trousers Small
  • Black Tshirt Small

…and we wanted to return all small and black products, we would try to search for ‘black small’.  This would return no results in the Magento catalog grid because no product titles have the words in that order.

Screen Shot 2014-10-31 at 16.26.31

The Solution

To solve this we created a little module to override Magento’s standard grid search with a more ‘fuzzy’ style search.  This actually turned out to be easier than expected and only required 3 files! So here’s how to do it…

Create a new module and create a basic config.xml file for it:

    1. Create a file and directory strucutre like this under app/code/local:

Screen Shot 2014-10-31 at 16.35.18

  1. In the Bigeyedeers/Catalogfuzz/config.xml file enter the following contents:
    
    <?xml version="1.0"?>
    
    <config>
    <modules>
    <Bigeyedeers_Catalogfuzz>
    <version>1.0.0.0</version>
    </Bigeyedeers_Catalogfuzz>
    </modules>
    <global>
    <blocks>
    <adminhtml>
    <rewrite>
    <catalog_product_grid>Bigeyedeers_Catalogfuzz_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
    </rewrite>
    </adminhtml>
    </blocks>
    </global>
    </config>
    

    In this config.xml file we are simply telling Magento about our module and also saying that we are going to rewrite the AdminHtml catalog_product_grid block with our own version.

  2. Now we need to add the functionality into our block file:
    
    <?php
    class Bigeyedeers_Catalogfuzz_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
    {
    
    protected function _addColumnFilterToCollection($column){
    
    $cond = $column->getFilter()->getCondition();
    
    if ($column->getId() == "name" && isset($cond)) {
    
    $filterOrig = $cond['like'];
    $filterReplaced = str_replace(" ", "%", $filterOrig);
    $newZendDbExpr = new Zend_Db_Expr($filterReplaced);
    $modifCond = array('like'=>$newZendDbExpr);
    $this->getCollection()->addFieldToFilter($column->getId() , $modifCond);
    
    }elseif ($column->getId() == 'websites') {
    $this->getCollection()->joinField('websites',
    'catalog/product_website',
    'website_id',
    'product_id=entity_id',
    null,
    'left');
    }else{
    parent::_addColumnFilterToCollection($column);
    }
    
    return $this;
    
    }
    
    }
    

     

    Without going into too much detail here we are basically just replacing any spaces in the search string with % characters, this is is a MySQL operator for performing a ‘like’ search.  This updated search string is then used in the collection query to return the product collection.  The product collection is then used to populate the catalog grid.

  3. Finally we need to activate our module by adding an xml file in app/etc/modules.  Simply create a file called Bigeyedeers_Catalogfuzz.xml and add this contents:
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <config>
    <modules>
    <Bigeyedeers_Catalogfuzz>
    <active>true</active>
    <codePool>local</codePool>
    </Bigeyedeers_Catalogfuzz>
    </modules>
    </config>
    

     

That’s it! You should now have a much more functional product name search in the admin catalog manager.