Davallen Posted September 14, 2013 Share Posted September 14, 2013 (edited) Hi, I'd like to show how many products we have in total in the search block so it says something like "Search within 2.343 products" Can anyone tell how to do this? I'm on version 1.5.3.1 Best regards Edited September 15, 2013 by Davallen (see edit history) 1 Link to comment Share on other sites More sharing options...
PascalVG Posted September 15, 2013 Share Posted September 15, 2013 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->Performanceto see the changes. (Don't forget to turn cache back ON afterwards!) Hope this helps, pascal 2 Link to comment Share on other sites More sharing options...
Davallen Posted September 15, 2013 Author Share Posted September 15, 2013 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 More sharing options...
Davallen Posted September 15, 2013 Author Share Posted September 15, 2013 I get this error Fatal error: Call to undefined method BlockSearch::getCacheId() in /var/www/freshsmoke.dk/public_html/modules/blocksearch/blocksearch.php on line 90 Link to comment Share on other sites More sharing options...
Davallen Posted September 15, 2013 Author Share Posted September 15, 2013 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 More sharing options...
PascalVG Posted September 15, 2013 Share Posted September 15, 2013 Yes, indeed, just add the red line. The return value was calculated a little different in your version (my code came from 1.5.5.0), but the idea was the same. Good job! Glad I could help! pascal 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now