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:
$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:
* 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:
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 .= " ";
}
$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.