VirtueMart Forum

VirtueMart 1.1.x [ Old version - no longer supported ] => Products, Prices, Tax and Categories VM 1.1 => Topic started by: Globalparts on November 21, 2009, 10:40:07 am

Title: Show the number of products in category (counting the subcategories products)
Post by: Globalparts on November 21, 2009, 10:40:07 am
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.
Title: Re: Show the number of products in category (counting the subcategories products)
Post by: Globalparts on November 28, 2009, 05:30:05 am
Please, I need help.

Nobody knows how to invoke products_in_category in a Module?

Thanks to the Comunity.
Title: Re: Show the number of products in category (counting the subcategories products)
Post by: megahz on January 25, 2010, 13:52:36 pm
i am facing the same problem.
anybody can help on this?

thanks.
Title: Re: Show the number of products in category (counting the subcategories products
Post by: elina on January 26, 2010, 09:19:44 am
Hello,

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

Did you find any solution??


thanks
Title: Re: Show the number of products in category (counting the subcategories products
Post by: rb on January 26, 2010, 18:23:29 pm
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
Title: Re: Show the number of products in category (counting the subcategories products)
Post by: Mustardseed on April 14, 2010, 19:35:30 pm
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?
Title: Re: Show the number of products in category (counting the subcategories products)
Post by: stinga on April 14, 2010, 21:18:58 pm
I would turn it off, do you really need it?
Title: Re: Show the number of products in category (counting the subcategories products
Post by: rb on April 16, 2010, 00:17:23 am
Mustardseed, did you correctly perform all 4 steps of this solution:
     http://forum.virtuemart.net/index.php?topic=64368.0
Title: Re: Show the number of products in category (counting the subcategories products)
Post by: bonbin on March 15, 2011, 07:02:28 am
try to look in here
http://forum.virtuemart.net/index.php?topic=44319.0