Increase the number of AJAX search results

PrestaShop by default has got possibility to turn on someting like AJAX search. AJAX search is a nice feature that allows your customer to see the search hints. It's very usefull tool which increasing your store usability. By default this tool shows up to 10 products. If you want to change the number of the products (especially if you want to increase the number of them) you have to modify the block search module template files and also PrestaShop core  (search class).

 

PrestaShop search tool and its hints

ajax search results prestashop

 

Modification of the core

In this case go to the /classes/ directory located in root dir of your PrestShop installation . Open the Search.php file. This is the search class - the most important thing in the whole "search" process. You've got there important functions related to the AJAX search feature. This is the code for AJAX search:

if ($ajax)
{
$sql = 'SELECT DISTINCT p.id_product, pl.name pname, cl.name cname,
cl.link_rewrite crewrite, pl.link_rewrite prewrite '.$score.'
FROM '._DB_PREFIX_.'product p
INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (
p.`id_product` = pl.`id_product`
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
)
'.Shop::addSqlAssociation('product', 'p').'
INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (
product_shop.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
)
WHERE p.`id_product` '.$product_pool.'
ORDER BY position DESC LIMIT 10';
return $db->
executeS($sql);
}

You have to change te LIMIT 10 value. You can increase or decrease the value of this param. This parameter defines the LIMIT of the results. So it is the MAXIMUM number of returned procuts. If you want to display 20 results - increase it to 20. So, i think that everything is clear in this case - append your changes and save the file.

 

It is time to the block search module modification

As in the header, now we will modify the search block module template (.tpl) files. In this case you have to open correct file. What I mean by correct file? I mean this file: modules/blocksearch/blocksearch-instantsearch.tpl. In this file you've got javascripts related to the both instantsearch and ajax search. Here is the function for search hints:

{if $ajaxsearch}
	<script type="text/javascript">
	// <![CDATA[
		$('document').ready( function() {
			$("#search_query_{$blocksearch_type}")
				.autocomplete(
					'{if $search_ssl == 1}{$link->getPageLink('search', true)}{else}{$link->getPageLink('search')}{/if}', {
						minChars: 3,
						max: 10,
						width: 500,
						selectFirst: false,
						scroll: false,
						dataType: "json",
						formatItem: function(data, i, max, value, term) {
							return value;
						},
						parse: function(data) {
							var mytab = new Array();
							for (var i = 0; i < data.length; i++)
								mytab[mytab.length] = { data: data[i], value: data[i].cname + ' > ' + data[i].pname };
							return mytab;
						},
						extraParams: {
							ajaxSearch: 1,
							id_lang: {$cookie->id_lang}
						}
					}
				)
				.result(function(event, data, formatted) {
					$('#search_query_{$blocksearch_type}').val(data.pname);
					document.location.href = data.product_link;
				})
		});
	// ]]>
	</script>
{/if}

 

Highlighted value: max: 10 is a value of displayed products. You can increase and decrease value of this parameter. If you want to display 15 products in hints - just instead max: 10 use max: 15, if you want to show 20 products, use max: 20. Save changes and refresh your store front end. Here is an effect:

 

 

 

 

author milos myszczuk
Article by Milosz Myszczuk PrestaShop expert, official PrestaShop community moderator. PHP developer, specialist in relative and spatial databases management, GIS Analyst, CEO & founder of VEKIA interactive agency. Read more about VEKIA company
If you like my articles and want much more valuable tips, feel free to send me donation
1.4 version 1.4.11 1.6 404 addon admin advertise ahref ajax alpha animation api app application authentication back office backup badge banner basics block bootstrap button cache carrier cart catalog category certificate changelog chat class clear client clip cms code colors columns comments configuration contact container content controller cookie counter country coupon css csv currency customer dashboard database debug default delete delivery desktop developer device disable discount displayNav displayTop download dynamic editor effect empty encrypt engine error exchange exclude export facebook faceshop fade fancoupon fancybox fanpage fatal feature feed field file fix fixed font footer free friendly url front ftp full gallery generate gift global godaddy google google+ gray grid groupon header help hide highlight homefeatured homepage hook hosting hover howto htaccess html html5 ID image import include input instagram installation integration iPhone issue javascript jquery kgb knowhow languages law left likebox link list livingsocial loading log login logo loyality mail mailing maintenance manufacturer marketing marquee mcrypt menu meta mobile modification module movie moving multilanguage multiupload must have mysql news newsletter notification number open graph order override page password performance PHP phpmyadmin picture pinterest plugin popup post prestashop prestashop 1.0 prestashop 1.1 prestashop 1.2 prestashop 1.3 prestashop 1.4 prestashop 1.5 price rules problem product profile promotion proslider purifier quantity query quick tip random rates register reinsurance release reporting reset responsive restore results ribbon rich text right sales search security seo service shadow share shipping shop shopmania slider smarty social networks SQL SSL statistics stock store style subcategory superuser support switcher tab tablet tag tax template text theme tinyMCE tips and tricks tpl tracking translations tree trends trigger tumblr tutorial twitter update upgrade upload variables video visits voucher vulnerability web2print wide widget width window wishlist wysiwyg youtube zip zopim