From 9be7d00d3a4b3382d3071691b696bd4d21674636 Mon Sep 17 00:00:00 2001 From: acidvertigo Date: Mon, 25 Aug 2014 15:01:41 +0200 Subject: [PATCH 1/4] Simplify count of products and subcategories using the dedicated functions in general.php --- catalog/index.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/catalog/index.php b/catalog/index.php index eda468819..59183cd0c 100644 --- a/catalog/index.php +++ b/catalog/index.php @@ -14,19 +14,17 @@ // the following cPath references come from application_top.php $category_depth = 'top'; + if (isset($cPath) && tep_not_null($cPath)) { - $categories_products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id = '" . (int)$current_category_id . "'"); - $categories_products = tep_db_fetch_array($categories_products_query); - if ($categories_products['total'] > 0) { + + if (tep_count_products_in_category($current_category_id, false) > 0) { $category_depth = 'products'; // display products - } else { - $category_parent_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$current_category_id . "'"); - $category_parent = tep_db_fetch_array($category_parent_query); - if ($category_parent['total'] > 0) { + } + + if (tep_has_category_subcategories($current_category_id) == true) { $category_depth = 'nested'; // navigate through the categories } else { $category_depth = 'products'; // category has no products, but display the 'no products' message - } } } From 6fa6fdc338683d057b0013cdfdb63ec3be010465 Mon Sep 17 00:00:00 2001 From: acidvertigo Date: Mon, 25 Aug 2014 15:10:03 +0200 Subject: [PATCH 2/4] remove implicit true check --- catalog/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catalog/index.php b/catalog/index.php index 59183cd0c..b6e8e4d36 100644 --- a/catalog/index.php +++ b/catalog/index.php @@ -21,7 +21,7 @@ $category_depth = 'products'; // display products } - if (tep_has_category_subcategories($current_category_id) == true) { + if (tep_has_category_subcategories($current_category_id)) { $category_depth = 'nested'; // navigate through the categories } else { $category_depth = 'products'; // category has no products, but display the 'no products' message From 7460ee77e84519583ec307913d8063e2f6be1eda Mon Sep 17 00:00:00 2001 From: acidvertigo Date: Mon, 25 Aug 2014 23:21:25 +0200 Subject: [PATCH 3/4] fix to not count also subcategories --- catalog/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catalog/index.php b/catalog/index.php index b6e8e4d36..c5b8477d8 100644 --- a/catalog/index.php +++ b/catalog/index.php @@ -17,7 +17,7 @@ if (isset($cPath) && tep_not_null($cPath)) { - if (tep_count_products_in_category($current_category_id, false) > 0) { + if (tep_count_products_in_category($current_category_id, false, false) > 0) { $category_depth = 'products'; // display products } From e2c106e30c59cf53efcb4481379a8fffad6d2e69 Mon Sep 17 00:00:00 2001 From: acidvertigo Date: Mon, 25 Aug 2014 23:25:22 +0200 Subject: [PATCH 4/4] Fix tep_count_products_in_category to check if the current category has subcategories without check subcategories --- catalog/includes/functions/general.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/catalog/includes/functions/general.php b/catalog/includes/functions/general.php index 7a87c5167..ee5f588c2 100644 --- a/catalog/includes/functions/general.php +++ b/catalog/includes/functions/general.php @@ -394,7 +394,7 @@ function tep_calculate_tax($price, $tax) { //// // Return the number of products in a category // TABLES: products, products_to_categories, categories - function tep_count_products_in_category($category_id, $include_inactive = false) { + function tep_count_products_in_category($category_id, $include_inactive = false, $count_subs = true) { $products_count = 0; if ($include_inactive == true) { $products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = p2c.products_id and p2c.categories_id = '" . (int)$category_id . "'"); @@ -404,11 +404,13 @@ function tep_count_products_in_category($category_id, $include_inactive = false) $products = tep_db_fetch_array($products_query); $products_count += $products['total']; - $child_categories_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$category_id . "'"); - if (tep_db_num_rows($child_categories_query)) { - while ($child_categories = tep_db_fetch_array($child_categories_query)) { - $products_count += tep_count_products_in_category($child_categories['categories_id'], $include_inactive); - } + if ($count_subs == true) { + $child_categories_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$category_id . "'"); + if (tep_db_num_rows($child_categories_query)) { + while ($child_categories = tep_db_fetch_array($child_categories_query)) { + $products_count += tep_count_products_in_category($child_categories['categories_id'], $include_inactive, $count_subs); + } + } } return $products_count;