Jump to content

Show number of products in category


Recommended Posts

Hi All,

 

I'm looking for a way of including a number next to the categories which indicates the number of products in that category.

 

I have found a paid for menu module which includes this as well as features i dont need and a free module website which has such a module but the page goes to a 404 error.

 

Anybody done this before?

 

Thanks,

 

Colin

Link to comment
Share on other sites

  • 4 months later...

Here is the solution tested and working in Prestashop 1.4.7.

 

You can follow these instructions below or just download the already modified files from the zip file attached here.

 

Hope this helps.

 

Open "blockcategories.php" in the folder /modules/blockcategories and find this lines of code (should start at line 140 or so):

public function getTree($resultParents, $resultIds, $maxDepth, $id_category = 1, $currentDepth = 0)
{
 global $link;
 $children = array();
 if (isset($resultParents[$id_category]) AND sizeof($resultParents[$id_category]) AND ($maxDepth == 0 OR $currentDepth < $maxDepth))
  foreach ($resultParents[$id_category] as $subcat)
$children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1);
 if (!isset($resultIds[$id_category]))
  return false;

 return array('id' => $id_category, 'link' => $link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']),
  'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'],
  'children' => $children);
}

 

And change them to this:

public function getTree($resultParents, $resultIds, $maxDepth, $id_category = 1, $currentDepth = 0)
{
 global $link;
 $children = array();
 if (isset($resultParents[$id_category]) AND sizeof($resultParents[$id_category]) AND ($maxDepth == 0 OR $currentDepth < $maxDepth))
  foreach ($resultParents[$id_category] as $subcat)
$children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1);
 if (!isset($resultIds[$id_category]))
  return false;

$ProductsCount = 0;
$ProductsCount = (int)Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'category_product WHERE id_category = '. $id_category);

 return array('id' => $id_category, 'link' => $link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']),
  'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'],
  'children' => $children, 'products' => $ProductsCount);
}

 

 

Then open the file "category_tree_branch.tpl" that you find in the same folder /modules/blockcategory and find this line (should be line 41):

  <a href="{$node.link}" {if isset($currentCategoryId) && ($node.id == $currentCategoryId)}class="selected"{/if} title="{$node.desc|escape:html:'UTF-8'}">{$node.name|escape:html:'UTF-8'}</a>

 

and change to this:

  <a href="{$node.link}" {if isset($currentCategoryId) && ($node.id == $currentCategoryId)}class="selected"{/if} title="{$node.desc|escape:html:'UTF-8'}">{$node.name|escape:html:'UTF-8'} ({$node.products})</a>

blockcategories_numbers_mod.zip

Link to comment
Share on other sites

  • 7 months later...

thanx, for hint. I modified Alexidro's code a little:

$ProductsCount = 0;

$ProductsCount = (int)Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'category_product WHERE id_category = '. $id_category);

I replaced by:

$productsNumber = 0;

$categoryObj = new Category($id_category);

$productsNumber = $categoryObj->getProducts((int)$params['cookie']->id_lang, 1, 10000000, null, null, true);

 

Since Prestashop has methods to get number of products in given category.

Hope it helps.

Edited by kostia_lev (see edit history)
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...