Jump to content

[Modification] Product Count on Category Navigation


Recommended Posts

Hey all,

Module: Category Navigation - Product Count
Version: 0.1
Tested On: 1.2.1
Cost: Free

This modification allows you to show the number of active products on your category navigation.

Example: Software (150)

To learn more, see it in action, and to download it please go to:
http://www.cytech-services.com/store/product.php?id_product=14

Hope this will be handy for a few people :-)

  • Like 1

Share this post


Link to post
Share on other sites
  • 2 weeks later...
  • 4 weeks later...
  • 2 weeks later...
  • 1 month later...
  • 2 months later...
  • 11 months later...

its ok, but i try do somethink like that:

category (11) [only one products is there] - where that number is all products from subcat + all product from category
supcat (4)
subcat (6)

some body done it?

Share this post


Link to post
Share on other sites

I just try to install this module, it looks cool :)
but after i return back to my old blockcategories.php and category-tree-branch.tpl, on the catagory tree, it always appear the "()", anyone know how can i remove it?

Share this post


Link to post
Share on other sites
  • 4 weeks later...

Open the file ../modules/blockcategories/category-tree-branch.tpl
Find the below code

{$node.name|escape:htmlall:'UTF-8'} ({$node.product_count}) 


Replace by this code

{$node.name|escape:htmlall:'UTF-8'}{if $node.product_count > 0}({$node.product_count}) {/if}



Best regards.

Share this post


Link to post
Share on other sites
  • 3 weeks later...
Open the file ../modules/blockcategories/category-tree-branch.tpl
Find the below code
{$node.name|escape:htmlall:'UTF-8'} ({$node.product_count}) 


Replace by this code

{$node.name|escape:htmlall:'UTF-8'}{if $node.product_count > 0}({$node.product_count}) {/if}



Best regards.



it works, thank you :)

Share this post


Link to post
Share on other sites
  • 1 month later...

also, if you want the number of products to appear only from the level 2 categories downwards (not on the 'main screen') , just replace the mentioned code for the category-branch-tree.tpl file to:

category-branch-tree.tpl



{if $node.bazinga_depth neq 1}{if $node.product_count > 0}({$node.product_count}){/if}{/if}



and add this line:

'bazinga_depth'=>$currentDepth



to your blockcategories.php file so it looks like this around 85 line:

blockcategories.php



return array('id' => $id_category, 'link' => $link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']), 'name' => Category::hideCategoryPosition($resultIds[$id_category]['name']), 'desc'=> $resultIds[$id_category]['description'], 'children' => $children, 'product_count' => $resultIds[$id_category]['product_count'],'bazinga_depth'=>$currentDepth);    

Share this post


Link to post
Share on other sites
  • 3 weeks later...
  • 1 month later...
also, if you want the number of products to appear only from the level 2 categories downwards (not on the 'main screen') , just replace the mentioned code for the category-branch-tree.tpl file to:

category-branch-tree.tpl



{if $node.bazinga_depth neq 1}{if $node.product_count > 0}({$node.product_count}){/if}{/if}



and add this line:

'bazinga_depth'=>$currentDepth



to your blockcategories.php file so it looks like this around 85 line:

blockcategories.php



return array('id' => $id_category, 'link' => $link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']), 'name' => Category::hideCategoryPosition($resultIds[$id_category]['name']), 'desc'=> $resultIds[$id_category]['description'], 'children' => $children, 'product_count' => $resultIds[$id_category]['product_count'],'bazinga_depth'=>$currentDepth);    




Thanks a million for posting this.

Share this post


Link to post
Share on other sites
  • 4 weeks later...

My AJAX Sliding Categories module has code that counts all products in subcategories. It modifies the getTree() function to count the products in each category and uses recursion to include subcategory counts in parent categories. It then caches the product numbers in the database for fast retrieval. Unfortunately, it's complicated code that I can't simply write here.

Share this post


Link to post
Share on other sites
  • 1 year later...

I would also like some code to count all products in any particular tree, all the way down, as a single sum, as jakubek noted:

 

- top cat (Candy Bars) has one product

- sub cat has four products

- sub-sub cat has six products

 

So, the final display for top cat looks like this:

 

Candy Bars (11)

 

Has anyone ever devised this type of summation code?

 

I'm looking for the exact same thing, has anyone found a solution to this and is willing to post it here? We'd appreciate it.

Edited by DylzEn (see edit history)

Share this post


Link to post
Share on other sites
  • 3 years later...

For add the count of product in subcategory you can do this (it's work in prestashop 1.6.0.9) :
 
1 : Create file who name category.php in overrhide/classes repertory who contain =>


<?php
/* this overhide is for add the products_nbr variable who have the count of product active in sub cateory recucivly */
class Category extends CategoryCore
{
public function getSubCategories($id_lang, $active = true)
{
$sql_groups_where = '';
$sql_groups_join = '';
if (Group::isFeatureActive())
{
$sql_groups_join = 'LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = c.`id_category`)';
$groups = FrontController::getCurrentCustomerGroups();
$sql_groups_where = 'AND cg.`id_group` '.(count($groups) ? 'IN ('.implode(',', $groups).')' : '='.(int)Group::getCurrent()->id);
}
 
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT c.*, cl.id_lang, cl.name, cl.description, cl.link_rewrite, cl.meta_title, cl.meta_keywords, cl.meta_description
FROM `'._DB_PREFIX_.'category` c
'.Shop::addSqlAssociation('category', 'c').'
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.(int)$id_lang.' '.Shop::addSqlRestrictionOnLang('cl').')
'.$sql_groups_join.'
WHERE `id_parent` = '.(int)$this->id.'
'.($active ? 'AND `active` = 1' : '').'
'.$sql_groups_where.'
GROUP BY c.`id_category`
ORDER BY `level_depth` ASC, category_shop.`position` ASC');
 
foreach ($result as &$row)
{
$row['id_image'] = Tools::file_exists_cache(_PS_CAT_IMG_DIR_.$row['id_category'].'.jpg') ? (int)$row['id_category'] : Language::getIsoById($id_lang).'-default';
$row['legend'] = 'no picture';
$row['products_nbr'] =  $this->getSubCatNbProductsRecursive($row['id_category'],  $row['nleft'],  $row['nright']);
 
}
 
return $result;
}
 
 
 
public function getSubCatNbProductsRecursive($id ,$nleft, $nright)
{
$nb_product_recursive = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(distinct(id_product))
FROM  `'._DB_PREFIX_.'category_product`
WHERE id_category IN (
SELECT c2.id_category
FROM `'._DB_PREFIX_.'category` c2
'.Shop::addSqlAssociation('category', 'c2').'
WHERE c2.nleft > '.(int)$nleft.'
AND c2.nright < '.(int)$nright.'
AND c2.active = 1
UNION SELECT '.(int)$id.'
)
AND id_product IN (
SELECT c3.id_product
FROM `'._DB_PREFIX_.'product` c3
'.Shop::addSqlAssociation('product', 'c3').'
WHERE c3.active = 1
UNION SELECT '.(int)$id.'
)
');
if (!$nb_product_recursive)
return 0;
return $nb_product_recursive;
}
}
 

 
2 :  add

{if $subcategory.products_nbr > 1} <h6>{$subcategory.products_nbr} Products</h6>{/if}

 after

<h5><a class="subcategory-name" href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'html':'UTF-8'}">{$subcategory.name|truncate:45:'...'|escape:'html':'UTF-8'|truncate:350}</a></h5>

in the category.tpl of your theme directory. 

 

3 :  delete the file class_index.php in cache directory

 

4 :  delete your cache smarty in the performance page in back office.

 

 

Share this post


Link to post
Share on other sites
  • 11 months later...

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...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More