Find Exact Phrase Or Word In Quick Search In Magento



In Magento quick search, when we search for word or a phrase it returns all the product contaning that word or phrase. Some time it returns all the products containing the word or phrase in the description or in the short description for example if we search for the phrase "shirts" it returns us T shirts as well as it return shirts also. It is very simple you can fix it easily the code fix is given below:

Step 1. Navigate to the app/code/core/Mage/CatalogSearch/Model/Mysql4/ and open the file Fulltext.php in the editor of your choice and search for the code below or look at the line 341 (Magento ver. 1.4.2.0) of the

 
<?php
    if ($like) {
       $likeCond = '(' . join(' OR ', $like) . ')';
    }
?>

Step 2. Change OR From AND

then the code will look like as

<?php
   if ($like) {
     $likeCond = '(' . join(' AND ', $like) . ')';
   }
?>

Note : For the newer version of the Magento like 1.7.2 you can find Fulltext.php file app/code/core/Mage/CatalogSearch/Model/Resource/

Step 3. Open this in the editor of your choice and search for the code or you can find it on the line 330.

<?php
   if ($like) {
       $likeCond = '(' . join(' OR ', $like) . ')';
   }
?>

Step 4. Now replace OR with the AND and then it will look like as given below:

<?php
   if ($like) {
     $likeCond = '(' . join(' AND ', $like) . ')';
   }
?>

Note : It is recommended that don’t change the Magento core files for the future Magento upgrade. So you can put your files into the app/code/local/Mage/CatalogSearch/Model/Mysql4/ or app/code/local/Mage/CatalogSearch/Model/Resource/ folder. So your file will be safe when ever you will do the Magento upgrade.

Now Reindex the data and refresh your cache. Now may be your problem has been solved.

Thanks and enjoy the happy coding in Magento