Jump to content

(SOLVED) Show total amount of products in search block


Davallen

Recommended Posts

Hi Dav,

 

Here we go:

edit class /Classes/Product.php (Backup first!):

 

add this function:

	public static function getTotalAmountOfProducts(Context $context = null, $activeOnly = true)
	{
		if (!$context)
			$context = Context::getContext();

		$front = true;
		if (!in_array($context->controller->controller_type, array('front', 'modulefront')))
			$front = false;

		$sql = 'SELECT p.`id_product`
				FROM `'._DB_PREFIX_.'product` p
				'.Shop::addSqlAssociation('product', 'p').'				
				WHERE 1 '.($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '').
				($activeOnly ? ' AND p.`active` = 1' : '');
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
		return count($result);
	}


and save.

 

Then edit file: modules/blocksearch/blocksearch.php (backup first):

find:

public function hookTop($params)

and add red lines:

 

public function hookTop($params)
{
if (Tools::getValue('search_query') || !$this->isCached('blocksearch-top.tpl', $this->getCacheId('blocksearch-top')))
 {
 $totalOfProducts = Product::getTotalAmountOfProducts();
 $this->calculHookCommon($params);
 $this->smarty->assign('blocksearch_type', 'top');
 $this->smarty->assign('totalProducts', $totalOfProducts);
 }
 
 return $this->display(__FILE__, 'blocksearch-top.tpl', Tools::getValue('search_query') ? null :
     $this->getCacheId('blocksearch-top'));
}
 

 

and save

 

Finally, copy the file: 

 modules/blocksearch/blocksearch-top.tpl

to: themes/<your theme folder>/modules/blocksearch/blocksearch-top.tpl

 

then edit this new file:

and change/add the red line: (almost at the end of the file)

 

<!-- Block search module TOP -->
<div id="search_block_top">
<form method="get" action="{$link->getPageLink('search')|escape:'html'}" id="searchbox">
<p>
<label for="search_query_top"><!-- image on background --></label>
<input type="hidden" name="controller" value="search" />
<input type="hidden" name="orderby" value="position" />
<input type="hidden" name="orderway" value="desc" />
<input class="search_query" type="text" id="search_query_top" name="search_query" value=
"search inside {$totalProducts} products"  onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'search inside {$totalProducts} products':this.value;" />
<input type="submit" name="submit_search" value="{l s='Search' mod='blocksearch'}" class="button" />
</p>
</form>
</div>
{include file="$self/blocksearch-instantsearch.tpl"}
{/if}
<!-- /Block search module TOP -->

 

and save.

This last code adds the sentense inside the search input field.

Note: When entering it deletes the string automatically. If they change their mind and don't add any text yet, the sentence will be displayed again.

 

 

You may need to (TEMPORARILY!!): 

- turn OFF your smarty cache and

- 'Template cache' set to "Recompile templates if the files have been updated"

in Advanced Parameters->Performance

to see the changes. (Don't forget to turn cache back ON afterwards!)

 

 

Hope this helps,

pascal

  • Like 2
Link to comment
Share on other sites

Hi Dav,

 

Here we go:

edit class /Classes/Product.php (Backup first!):

 

add this function:

	public static function getTotalAmountOfProducts(Context $context = null, $activeOnly = true)
	{
		if (!$context)
			$context = Context::getContext();

		$front = true;
		if (!in_array($context->controller->controller_type, array('front', 'modulefront')))
			$front = false;

		$sql = 'SELECT p.`id_product`
				FROM `'._DB_PREFIX_.'product` p
				'.Shop::addSqlAssociation('product', 'p').'				
				WHERE 1 '.($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '').
				($activeOnly ? ' AND p.`active` = 1' : '');
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
		return count($result);
	}


and save.

 

Then edit file: modules/blocksearch/blocksearch.php (backup first):

find:

public function hookTop($params)

and add red lines:

 

public function hookTop($params)
{
if (Tools::getValue('search_query') || !$this->isCached('blocksearch-top.tpl', $this->getCacheId('blocksearch-top')))
 {
 $totalOfProducts = Product::getTotalAmountOfProducts();
 $this->calculHookCommon($params);
 $this->smarty->assign('blocksearch_type', 'top');
 $this->smarty->assign('totalProducts', $totalOfProducts);
 }
 
 return $this->display(__FILE__, 'blocksearch-top.tpl', Tools::getValue('search_query') ? null :
     $this->getCacheId('blocksearch-top'));
}
 

 

and save

 

Finally, copy the file: 

 modules/blocksearch/blocksearch-top.tpl

to: themes/<your theme folder>/modules/blocksearch/blocksearch-top.tpl

 

then edit this new file:

and change/add the red line: (almost at the end of the file)

 

<!-- Block search module TOP -->
<div id="search_block_top">
<form method="get" action="{$link->getPageLink('search')|escape:'html'}" id="searchbox">
<p>
<label for="search_query_top"><!-- image on background --></label>
<input type="hidden" name="controller" value="search" />
<input type="hidden" name="orderby" value="position" />
<input type="hidden" name="orderway" value="desc" />
<input class="search_query" type="text" id="search_query_top" name="search_query" value=
"search inside {$totalProducts} products"  onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'search inside {$totalProducts} products':this.value;" />
<input type="submit" name="submit_search" value="{l s='Search' mod='blocksearch'}" class="button" />
</p>
</form>
</div>
{include file="$self/blocksearch-instantsearch.tpl"}
{/if}
<!-- /Block search module TOP -->

 

and save.

This last code adds the sentense inside the search input field.

Note: When entering it deletes the string automatically. If they change their mind and don't add any text yet, the sentence will be displayed again.

 

 

You may need to (TEMPORARILY!!): 

- turn OFF your smarty cache and

- 'Template cache' set to "Recompile templates if the files have been updated"

in Advanced Parameters->Performance

 

to see the changes. (Don't forget to turn cache back ON afterwards!)

 

 

Hope this helps,

pascal

 

Hi Pascal,

 

Thank you very much for this guidance, I had a problem though. In my blocksearch.php my public function looked like this

public function hookTop($params)
	{
		$this->calculHookCommon($params);
		$this->smarty->assign('blocksearch_type', 'top');
		return $this->display(__FILE__, 'blocksearch-top.tpl');
	}

and I changed it to this

public function hookTop($params){
	
	if (Tools::getValue('search_query') || !$this->isCached('blocksearch-top.tpl', $this->getCacheId('blocksearch-top')))
	 {
		 $totalOfProducts = Product::getTotalAmountOfProducts();
		 $this->calculHookCommon($params);
		 $this->smarty->assign('blocksearch_type', 'top');
		 $this->smarty->assign('totalProducts', $totalOfProducts);
	 }
	 
	 return $this->display(__FILE__, 'blocksearch-top.tpl', Tools::getValue('search_query') ? null :
	     $this->getCacheId('blocksearch-top'));
	}

And not only the red line since

return $this->display(__FILE__, 'blocksearch-top.tpl', Tools::getValue('search_query') ? null :
     $this->getCacheId('blocksearch-top'));

wans't there.

 

Am I doing anything wrong?

Link to comment
Share on other sites

I made it work. Thank you very much!

 

Just added the red lines as you said, so now it's

public function hookTop($params)
	{
		$totalOfProducts = Product::getTotalAmountOfProducts();
		$this->calculHookCommon($params);
		$this->smarty->assign('blocksearch_type', 'top');
		$this->smarty->assign('totalProducts', $totalOfProducts);
		return $this->display(__FILE__, 'blocksearch-top.tpl');
	}

If anyone else need it!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...