Author Topic: Number of products in category does counted only one level deep  (Read 8544 times)

vmnoob

  • Beginner
  • *
  • Posts: 18
I have a quite detailed tree of categories. As result, products are placed in categories 3 level deep from root not counted and there is (0) on the main page.

I found code what does this calculation and it looking only for products in base and in it's child category, but doesn't going any deeper.
I'm not so good in SQL, so this makes me have headache. Anybody can help me to fix it? It would be enough to count products in child categories two levels deep.

Code: [Select]
/**
* 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 )) {
$db = new ps_DB;
$q = "SELECT category_child_id FROM #__{vm}_category_xref ";
$q .= "WHERE category_parent_id='$category_id' ";
$db->query($q);
while( $db->next_record() ) {
$num += ps_product_category::product_count($db->f("category_child_id"));
}
}

return " ($num) ";
}
else
return ( "" );

}

vmnoob

  • Beginner
  • *
  • Posts: 18
Re: Number of products in category does counted only one level deep
« Reply #1 on: August 10, 2008, 07:54:52 am »
This thread could be merged with this one:
http://forum.virtuemart.net/index.php?topic=40034.0

CTPAHHuK

  • Beginner
  • *
  • Posts: 5
Re: Number of products in category does counted only one level deep
« Reply #2 on: August 12, 2008, 15:18:58 pm »
open
administrator\components\com_virtuemart\classes\ps_product_category.php

and change function function products_in_category :

Code: [Select]
function products_in_category( $category_id ) {
if( PSHOP_SHOW_PRODUCTS_IN_CATEGORY == '1' ) {
$num = ps_product_category::product_count($category_id);
if( empty($num) && ps_product_category::has_childs( $category_id )) {
$db = new ps_DB;
$q = "SELECT category_child_id FROM #__{vm}_category_xref ";
$q .= "WHERE category_parent_id='$category_id' ";
$db->query($q);
while( $db->next_record() ) {
$num += ps_product_category::product_count($db->f("category_child_id"));
}
}

return " ($num) ";
}
else
return ( "" );

}

to
Code: [Select]
function prod_in_cat_recursive( $category_id ) {
$num=0;
if (ps_product_category::has_childs( $category_id )) {
$db = new ps_DB;
$q = "SELECT category_child_id FROM #__{vm}_category_xref ";
$q .= "WHERE category_parent_id='$category_id' ";
$db->query($q);
while( $db->next_record() ) {
$num += ps_product_category::prod_in_cat_recursive($db->f("category_child_id"));
}
}
$num += ps_product_category::product_count($category_id);
return $num;
}

function products_in_category( $category_id ) {
if( PSHOP_SHOW_PRODUCTS_IN_CATEGORY == '1' ) {
$num = ps_product_category::prod_in_cat_recursive( $category_id );
return " ($num) ";
}
else
return "";
}

vmnoob

  • Beginner
  • *
  • Posts: 18
Re: Number of products in category does counted only one level deep
« Reply #3 on: August 12, 2008, 19:12:42 pm »
Thank you very much! It working!

Спасибо, братан! ;)

weballabout

  • Beginner
  • *
  • Posts: 4
Re: Number of products in category does counted only one level deep
« Reply #4 on: March 23, 2012, 10:48:14 am »
Thank you this is still saving life's