Author Topic: Show the number of products in category (counting the subcategories products)  (Read 8072 times)

Globalparts

  • Beginner
  • *
  • Posts: 7
I have the code below on a "Category" module. The problem is that this code only shows de number of products of the fisrt category. If the category doesn't have any product but the category has child categories with products, it shows (0) despite of there are products into the child categories. The problem is in:

Code: [Select]
$data .= "<li><a href=\"$linkc\" title=\"" . $child[$i]->name . "\">" . $child[$i]->name . " (" . number_format($child[$i]->total, 0, "", ".") . ")</a></li>";

I have put the code below in the ps_product_category.php file but I don't know how to use/invoke in that case:

Code: [Select]
* recursive function getting all child categories of the parent category
*
* @param int $category_id
* @param int $interation (iterations of recursive function)
*/
function get_all_child_categories($category_id,$iteration){
$db = new ps_DB;
if($iteration==0){
$this->catArray=array();
}
$q = "SELECT category_child_id FROM #__{vm}_category_xref ";
$q .= "WHERE category_parent_id='$category_id' ";
$db->query($q);
$this->catArray[] =  $category_id;
while( $db->next_record() ) {
$catArray[]=$db->f("category_child_id");
$this->get_all_child_categories($db->f("category_child_id"),$iteration+1);
}
  return $this->catArray;
}

/**
* Shows the Number of Products in category $category_id
*
* @param int $category_id
* @return string The number in brackets
*/

function products_in_category( $category_id ) {
if( PSHOP_SHOW_PRODUCTS_IN_CATEGORY == '1' || vmIsAdminMode() ) {
$num = ps_product_category::product_count($category_id);
if( empty($num) && ps_product_category::has_childs( $category_id )) {
$catArray=array();
/*selecting parent childs*/
ps_product_category::get_all_child_categories($category_id,0);
foreach($this->catArray as $catID){
$num += ps_product_category::product_count($catID);
}
}
return " ($num) ";
}
else
return ( "" );


}

The full code of the file is:

Code: [Select]
       defined('_JEXEC') or die('Restricted access');

$menu = &JSite::getMenu();
$items = $menu->getItems('link', 'index.php?option=com_virtuemart');
$itemid = isset($items[0]) ? '&Itemid='.$items[0]->id : '';

$modtype = $params->get('modtype');
$showtitle = $params->get('showtitle');
$showdesc = $params->get('showdesc');
$showsubcat = $params->get('showsubcat');
$showthumb = $params->get('showthumb');
$col = $params->get ('num_cols', 2);
$colwidth = floor(100/$col);
$x = 0;
$y = 1;
$row = ceil(count($list)/$col);
$total = $col*$row;
$rest = ($total-count($list))+1;

if($modtype == 0){
$headtag = "<link rel=\"stylesheet\" href=\"" . JURI::root() . "/modules/mod_moonindexvm/tmpl/moonindexvm.css\" type=\"text/css\" />";
}else{
$headtag = "<link rel=\"stylesheet\" href=\"" . JURI::root() . "/modules/mod_moonindexvm/tmpl/moonindexvm_menu.css\" type=\"text/css\" />";
}
$mainframe->addCustomHeadTag($headtag);

if($list){
if($modtype == 0){
$data  = "<div class=\"productthumb\">";
$data .= '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
for($a=0; $a<$total; $a++){
if($y == 1){
$data .= '<tr valign="top"><td width="' . $colwidth . '%">';
}else{
$data .= '<td width="' . $colwidth . '%">';
}


if(isset($list[$a]->id)){
$linkp = JRoute::_('index.php?option=com_virtuemart&page=shop.browse&category_id='.$list[$a]->id.$itemid, false);
$data .= "<div class=\"pthumb\">";

if($showthumb == 1){
$class = " bbb";
}else{
$class = "";
}

if($showtitle == 1){
$data .= "<a href=\"$linkp\" title=\"" . $list[$a]->name . "\" id=\"productthumbbig$x\" class=\"productthumbbigtitle\">";
$data .= "<span>" . $list[$a]->name . "</span>";
$data .= "</a>";
}

if($showthumb == 1){
$data .= "<a href=\"$linkp\" title=\"" . $list[$a]->name . "\" id=\"productthumbbig$x\" class=\"productthumbbig\">";
$data .= "<div class=\"pthumbimg\"><img src='" . JURI::base(true) . "/components/com_virtuemart/shop_image/category/" . $list[$a]->filename . "' /></div>";
$data .= "</a>";
}

if($showdesc == 1 || $showsubcat == 1){
$data .= "<div class='detail" . $class . "'>";

if($showdesc == 1){ $data .= "<div class='desc'>" . $list[$a]->desc . "</div>";}

if($showsubcat == 1){
$child = modTPIndexHelper::getListChild($params, $list[$a]->id);
if(count($child) > 0){
$data .= "<ul>";
for($i=0; $i<count($child); $i++){
$linkc = JRoute::_('index.php?option=com_virtuemart&page=shop.browse&category_id='.$child[$i]->id.$itemid, false);
$data .= "<li><a href=\"$linkc\" title=\"" . $child[$i]->name . "\">" . $child[$i]->name . " (" . number_format($child[$i]->total, 0, "", ".") . ")</a></li>";
}
$data .= "</ul>";
}
}
$data .= "</div>";
$data .= "<div class='clear'></div>";
}
$data .= "</div>";
}else{
$data .= "&nbsp;";
}

$x++;
if($y == $col){
$data .= '</td></tr>';
$y = 1;
}else{
$data .= '</td>';
$y++;
}
}
$data .= "</table>";
}else{
$data  = "<div class=\"productthumb_menu\">";
$data .= '<ul class="parent">';
for($a=0; $a<$total; $a++){
if(isset($list[$a]->id)){
$data .= '<li>';
$linkp = JRoute::_('index.php?option=com_virtuemart&page=shop.browse&category_id='.$list[$a]->id.$itemid, false);
$data .= "<div class=\"pthumb\">";

if($showthumb == 1){
$class = " bbb";
}else{
$class = "";
}

$data .= "<a href=\"$linkp\" title=\"" . $list[$a]->name . "\" id=\"productthumbbig$x\" class=\"productthumbbigtitle\">";
$data .= "<span>" . $list[$a]->name . "</span>";
$data .= "</a>";

if($showthumb == 1){
$data .= "<a href=\"$linkp\" title=\"" . $list[$a]->name . "\" id=\"productthumbbig$x\" class=\"productthumbbig\">";
$data .= "<img src='" . JURI::base(true) . "/components/com_virtuemart/shop_image/category/" . $list[$a]->filename . "' />";
$data .= "</a>";
}

if($showdesc == 1 || $showsubcat == 1){
$data .= "<div class='detail" . $class . "'>";

if($showdesc == 1){ $data .= "<div class='desc'>" . $list[$a]->desc . "</div>";}

if($showsubcat == 1){
$child = modTPIndexHelper::getListChild($params, $list[$a]->id);
if(count($child) > 0){
$data .= "<ul>";
for($i=0; $i<count($child); $i++){
$linkc = JRoute::_('index.php?option=com_virtuemart&page=shop.browse&category_id='.$child[$i]->id.$itemid, false);
$data .= "<li><a href=\"$linkc\" title=\"" . $child[$i]->name . "\">" . $child[$i]->name . " (" . number_format($child[$i]->total, 0, "", ".") . ")</a></li>";
}
$data .= "</ul>";
}
}
$data .= "</div>";
$data .= "<div class='clear'></div>";
}
$data .= "</div>";
$data .= '</li>';
}

$x++;
}
$data .= "</ul>";
}
$data .= "</div>";
}else{
$data = "";
}
echo $data;
?>

I would apreciate some help.

Globalparts

  • Beginner
  • *
  • Posts: 7
Please, I need help.

Nobody knows how to invoke products_in_category in a Module?

Thanks to the Comunity.

megahz

  • Beginner
  • *
  • Posts: 2
i am facing the same problem.
anybody can help on this?

thanks.

elina

  • Beginner
  • *
  • Posts: 3
Hello,

i am having this problem... ??? Virtuemart module doesn't count products from subcategories.

Did you find any solution??


thanks

rb

  • Advanced
  • Full Member
  • *****
  • Posts: 1503
If you have just one level of subcategories below your top level, this hack will count products within your subcategories:

Make a copy of, then edit this file:
     administrator/components/com_virtuemart/classes/ps_product_category.php

In the function "products_in_category", find this code around line 1004:
Code: [Select]
if( empty($num) && ps_product_category::has_childs( $category_id )) {
Replace it with this line:
Code: [Select]
if( ps_product_category::has_childs( $category_id )) {

If you have more than 1 level of subcategories, install this hack:
http://forum.virtuemart.net/index.php?topic=64368.0

Mustardseed

  • Beginner
  • *
  • Posts: 2
Hi,
Ihave the same problem. I have tried the solutions mentioned, but unfortunately it didn't work for me. I still have:
Main category (0)
   Sub-category (0)
      Sub-category A (8)
      Sub-category B (12)

I have tried with only one level of sub-category, but the count for the main category remains (0).

Does anyone have an idea, please?

stinga

  • Contributing Developer
  • Full Member
  • *
  • Posts: 872
    • Squangle ltd
I would turn it off, do you really need it?
Stinga.
614869 products in 747 categories with 15749 products in 1 category.
                                             Document Complete   Fully Loaded
                Load Time First Byte Start Render   Time      Requests      Time      Requests
First View     2.470s     0.635s     1.276s          2.470s       31            2.470s      31
Repeat View  1.064s     0.561s     1.100s          1.064s       4             1.221s       4

rb

  • Advanced
  • Full Member
  • *****
  • Posts: 1503
Mustardseed, did you correctly perform all 4 steps of this solution:
     http://forum.virtuemart.net/index.php?topic=64368.0

bonbin

  • Beginner
  • *
  • Posts: 25