Post by Heilong » Tue Nov 11, 2008 10:25 am

Hi everyone,

Anyone knows how to display the total number of products when we see the subcategories thumbnail, total number of products for that subcategory.

Any help ?

Thanks

New member

Posts

Joined
Mon Sep 01, 2008 5:57 pm

Post by fido-x » Tue Nov 11, 2008 5:31 pm

The easiest way is to make a query of the product_to_category table to find how many products are associated with the category.
In "catalog/controller/category.php", around lines 50-60, you should find something similar to the following:-

Code: Select all

$results = $database->getRows("select * from category c left join category_description cd on (c.category_id = cd.category_id) left join image i on (c.image_id = i.image_id) where c.parent_id = '" . (int)(!$request->get('path') ? '0' : (int)end(explode('_', $request->get('path')))) . "' and cd.language_id = '" . (int)$language->getId() . "' order by sort_order");
foreach ($results as $result) {
   // count products in category inserted by Fido-X
   $num_products = $database->countRows("select * from product_to_category where category_id = '" . (int)$result['category_id'] . "'");
   // end of insert
   $category_data[] = array(
      // inserted by Fido-X
      'product_count' = > $num_products,
      // end of insert
      'name'  => $result['name'],
      'description' => html_entity_decode($result['description']),
      'href'  => $url->href('category', FALSE, array('path' => ($request->get('path')) ? $request->get('path') . '_' . $result['category_id'] : $result['category_id'])),
      'thumb' => $image->resize($result['filename'], $config->get('config_image_width'), $config->get('config_image_height'))
   );
}
$view->set('categories', $category_data);
// inserted by Fido-X
$view->set('text_count', $language->get('text_count'));
// end of insert
Insert the commented bits.

Then, in "catalog/language/english/controller/category.php", insert the following:-

Code: Select all

$_['text_count'] = 'Products in Category: ';
Then, in "catalog/template/default/content/category.tpl" insert the following:-

Code: Select all

<?php echo $text_count; ?><?php echo $category['product_count']; ?>
below the thumbnail.

That should give you something like what you're after.

EDIT
The "$num_products" query in the above will count ALL products associated with the category, whether the product is enabled or not. Change the query to the following to count only those that are enabled:-

Code: Select all

$num_products = $database->countRows("select * from product_to_category p2c left join product p on p2c.product_id = p.product_id where p.status = '1' and p2c.category_id = '" . (int)$result['category_id'] . "'");
Fido-X.
Last edited by fido-x on Tue Nov 11, 2008 6:08 pm, edited 1 time in total.

Image
Modules for OpenCart 2.3.0.2
Homepage Module [Free - since OpenCart 0.7.7]
Multistore Extensions
Store Manager Multi-Vendor/Multi-Store management tool

If you're not living on the edge ... you're taking up too much space!


User avatar
Expert Member

Posts

Joined
Sat Jun 28, 2008 1:09 am
Location - Tasmania, Australia

Post by Qphoria » Tue Nov 11, 2008 10:55 pm

very nice breakdown Fido :)

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am
Who is online

Users browsing this forum: No registered users and 6 guests