Better search in OpenCart – search product descriptions by default

By default, the search function in OpenCart is pretty limited and only searches product names. In Advanced Search there is an option to Search in product descriptions. Here’s how to make OpenCart search in product descriptions, and in all categories, by default.

If you are running a large shop with thousands of products you don’t want to search the product descriptions by default as it will take a long time. However, if you’ve only got a small or medium-sized shop you can give your visitors a better search experience by enabling search in product description by default.

The javascript for the search, located near the base of the header template file, header.tpl is as follows:

<script type="text/javascript"><!--
$('#search input').keydown(function(e) {
if (e.keyCode == 13) {
moduleSearch();
}
});
function moduleSearch() {
url = 'index.php?route=product/search';
var filter_keyword = $('#filter_keyword').attr('value')
if (filter_keyword) {
url += '&keyword=' + encodeURIComponent(filter_keyword);
}
var filter_category_id = $('#filter_category_id').attr('value');
if (filter_category_id) {
url += '&category_id=' + filter_category_id;
}
location = url;
}
//--></script>

If you want to search product descriptions and all categories by default, change this javascript to the following:

<script type="text/javascript"><!--
$('#search input').keydown(function(e) {
if (e.keyCode == 13) {
moduleSearch();
}
});
function moduleSearch() {
url = 'index.php?route=product/search';
var filter_keyword = $('#filter_keyword').attr('value')
if (filter_keyword) {
url += '&keyword=' + encodeURIComponent(filter_keyword) + '&description=true';
}
location = url;
}
//--></script>

Notice we’ve added “description=true” and removed the category filter. That’s all you need to do. If you do so, I’d recommend deleting the categories select box from the search div (div9 in the default template). I’d also lose the link to Advanced Search as you are already providing an advanced search.

This entry was posted in OpenCart Mods. Bookmark the permalink.

7 Responses to Better search in OpenCart – search product descriptions by default

  1. Matt89fe says:

    nice tip again ;)

  2. jam says:

    dear my friend !
    I want to search by price but i do not know how to do ! please tell me ?

  3. Craig says:

    Hi Jam, you can’t search by price at present. I believe that feature is going to be in version 1.5. You could install QPhoria’s All Products module (http://theqdomain.com/ocstore/allprod). You can then sort all products by Price. Or you could create some price Categories and put your products in there e.g. Have 3 Categories such as £0-£20, £20-£50, £50 and more. Hope that helps.

  4. Renato says:

    merci :-)

  5. JohnnyVoners says:

    hi i’m trying this right now,
    any extra info on how to delete the advanced search link?
    thanks,
    pieter

  6. JohnnyVoners says:

    I would also like to have the search function include the models, is this possible?

  7. Craig says:

    Hi Johnny, you can edit the header.tpl to remove the advanced search link – just find this and delete it:
    <a href="<?php echo str_replace('&', '&amp;', $advanced); ?>"><?php echo $text_advanced; ?></a>

    I’m afraid I haven’t investigated searching in models.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>