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:
…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.
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:
<?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.
<?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.
<?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.
Magento is the leading solution for eCommerce, and we’re specialists. Magento is easy-to-use, completely customisable and endlessly scalable.
We offer custom web development services for any requirement. Manufacturing from scratch, we deliver a project entirely tailored to your needs.
Working with brands nationally from our offices in Cardiff and Exeter, our tenacious team of designers and developers deliver sophisticated results every time.