Page 1 of 2
number of products in brackets next to category?
Posted: Wed Oct 14, 2009 2:07 am
by DannyMacD
Hello,
like this site has,
http://www.shrimantech.com/opencart/dem ... ommon/home
i would like to know what code is needed to show the number of products in that category.
example
Desktops (20)
- Mac (12)
- PC (8)
many thanks
Danny
Re: number of products in brackets next to category?
Posted: Wed Oct 21, 2009 8:28 pm
by readyman
UPDATED FOR CLARITY BY QPHORIA
1. EDIT: catalog/controller/module/category.php
2. FIND:
Code: Select all
if ($this->category_id == $result['category_id']) {
$output .= '<a href="' . $this->model_tool_seo_url->rewrite($this->url->http('product/category&path=' . $new_path)) . '"><b>' . $result['name'] . '</b></a>';
} else {
$output .= '<a href="' . $this->model_tool_seo_url->rewrite($this->url->http('product/category&path=' . $new_path)) . '">' . $result['name'] . '</a>';
}
3. REPLACE WITH:
Code: Select all
$this->load->model('catalog/product');
$getTotal = '('. $this->model_catalog_product->getTotalProductsByCategoryId($result['category_id']) .')';
if ($this->category_id == $result['category_id']) {
$output .= '<a href="' . $this->model_tool_seo_url->rewrite($this->url->http('product/category&path=' . $new_path)) . '"><b>' . $result['name'] . '</b></a>'. $getTotal;
} else {
$output .= '<a href="' . $this->model_tool_seo_url->rewrite($this->url->http('product/category&path=' . $new_path)) . '">' . $result['name'] . '</a>' . $getTotal;
}
Re: number of products in brackets next to category?
Posted: Thu Oct 22, 2009 4:29 am
by digitalchaos
is there any way to color these numbers and brackets
Re: number of products in brackets next to category?
Posted: Wed Dec 02, 2009 6:09 am
by Qphoria
I've cleaned up this thread to remove all the confusing posts and questions/spam. I've updated readyman's thread to be a bit clearer and to add the model load step as that is needed.
The code above is still credited to readyman but it should work for all versions of OpenCart the same way. I've tested it out and it works fine in 1.3.2 and 1.3.4
Re: number of products in brackets next to category?
Posted: Wed Dec 02, 2009 8:25 am
by DoctorQ
test for v 1.3.3 ---> OK!
Re: number of products in brackets next to category?
Posted: Wed Dec 02, 2009 6:36 pm
by leprimo
V.1.3.4, oh,what have I done?
Components (0) but subcategory Monitors(3)

Re: number of products in brackets next to category?
Posted: Wed Dec 02, 2009 8:55 pm
by fido-x
leprimo wrote:V.1.3.4, oh,what have I done?
Components (0) but subcategory Monitors(3)

That's right. The "Components" category doesn't have any products, so it doesn't count any.
Re: number of products in brackets next to category?
Posted: Wed Dec 02, 2009 9:34 pm
by leprimo
Mmhh, but only silly users click on categories without entry.
I think that's not really good.
Re: number of products in brackets next to category?
Posted: Wed Dec 02, 2009 10:01 pm
by fido-x
You missed the point. Only categories that have PRODUCTS will display a PRODUCT count. Categories that have sub-categories but no products will display a zero value because the products are in the sub-categories.
You will note that, in the default set up (as per your attached image), there are 15 products in the "Desktops" category. There are also 2 sub-categories (PC and MAC) that are not counted!
Sounds like what you want is something that counts sub-categories as well as products.
Re: number of products in brackets next to category?
Posted: Mon Mar 29, 2010 7:59 am
by pepiw
Hi,
this is the code modified for v1.4.4. Tested and work.
Code: Select all
$this->load->model('catalog/product');
$getTotal = '('. $this->model_catalog_product->getTotalProductsByCategoryId($result['category_id']) .')';
if ($this->category_id == $result['category_id']) {
$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path) . '"><b>' . $result['name'] . '</b></a>'. $getTotal;
} else {
$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path) . '">' . $result['name'] . '</a>' . $getTotal;
}
But still the main category show (0) although in the subcategory are eg 14 products.
Like this:
Category_A (0)
Subcategory_A (3)
Subcategory_B (5)
Subcategory_C (6)
I want shows something like that:
Category_A (14)
Subcategory_A (3)
Subcategory_B (5)
Subcategory_C (6)
How I do this? Help!
Re: number of products in brackets next to category?
Posted: Mon May 31, 2010 1:12 pm
by Perthaussie
leprimo wrote:Mmhh, but only silly users click on categories without entry.
I think that's not really good.
I have added a message at the top of the category list to let customers know there may be items in sub-categories of empty top level categories.
Edit the file: catalog/view/theme/XXXXX/template/module/category.tpl
Code: Select all
<div id="category" class="middle"><?php echo 'Empty categories may have items in subcategories';?></br></br><?php echo $category; ?></div>
The category count idea seemed such a good idea that I thought the message would overcome the empty category drawback.
You can see the benefit better in the demo store in my signature.
Re: number of products in brackets next to category?
Posted: Tue Jul 13, 2010 8:32 pm
by dramony
Can someone please help us make this work on the latest version?
Thanks!
Re: number of products in brackets next to category?
Posted: Tue Jul 20, 2010 7:42 pm
by Trucifix
I just added the later of the two codes to version 1.4.8b and it appears to be working fine.
Re: number of products in brackets next to category?
Posted: Tue Jul 20, 2010 8:42 pm
by Maansy
I want to show something like that:
Category_A (14)
Subcategory_A (3)
Subcategory_B (5)
Subcategory_C (6)
How do I do that?
thanks
Re: number of products in brackets next to category?
Posted: Tue Jul 20, 2010 9:00 pm
by Perthaussie
Maansy wrote:I want to show something like that:
Category_A (14)
Subcategory_A (3)
Subcategory_B (5)
Subcategory_C (6)
How do I do that?
thanks
As explained earlier in this thread, it is not possible.
Re: number of products in brackets next to category?
Posted: Sat Aug 07, 2010 9:27 am
by Maansy
Perthaussie wrote:Maansy wrote:I want to show something like that:
Category_A (14)
Subcategory_A (3)
Subcategory_B (5)
Subcategory_C (6)
How do I do that?
thanks
As explained earlier in this thread, it is not possible.
NOTHING is not possible

one day someone will figure it out
Re: number of products in brackets next to category?
Posted: Fri Aug 20, 2010 1:44 pm
by dramony
I agree with Maansy.
If it worked on earlier versions, how come it wont on the latest?
Re: number of products in brackets next to category?
Posted: Fri Aug 20, 2010 2:18 pm
by cevarief
Yeah it's possible, only slow down opencart 1 or 2 sec (depend on how many sub folder) since the code should traverse its sub folder to count how many product in it

Re: number of products in brackets next to category?
Posted: Thu Sep 09, 2010 3:11 am
by celestial
that is working for v1.4.9???
Re: number of products in brackets next to category?
Posted: Tue Oct 12, 2010 4:07 am
by virgil
Does someone please know how you can make it like this for v1.4.9.1:
Category_A (14)
Subcategory_A (3)
Subcategory_B (5)
Subcategory_C (6)