Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Category box enhancement hide empty categories


genata93

Recommended Posts

Posted

Hello,

I am currently using the Category box enhancement contribution and I want it to automatically hide empty categories(categories that either have no products in them or there are products with 0 quantity).

 function tep_get_paths($categories_array = '', $parent_id = 0, $level = 0, $path='') {
global $languages_id;

$check_query = tep_db_query("select * from " . TABLE_CATEGORIES);
$check = tep_db_fetch_array($check_query);
if (isset($check['status_categ'])) {
  $catstatus = " and c.status_categ = '1'";
} else {
  $catstatus = '';
}

if (!is_array($categories_array)) $categories_array = array();
$categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where parent_id = " . (int)$parent_id . $catstatus . " and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name");
while ($categories = tep_db_fetch_array($categories_query)) {
  if (SHOW_COUNTS == 'true') {
	$count_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 p.products_status = '1' and p2c.categories_id = " . (int)$categories['categories_id']);
	$products = tep_db_fetch_array($count_query);
	$count = $products['total'];
  } else {
	$count = 0;
  }
 $categories_array[$categories['categories_id']] = array('name' => $categories['categories_name'],
							  'path' => $path . $categories['categories_id'],
							  'indent' => str_repeat('   ', $level),
							  'has_subcat' => false,
							  'prod_count' => $count,
							  'parent' => $parent_id);
  $categories_array[$parent_id]['has_subcat'] = true;
  if ($categories['categories_id'] != $parent_id) {
	$categories_array = tep_get_paths($categories_array, $categories['categories_id'], $level + 1, $path . $categories['categories_id'] . '_');
  }
}

return $categories_array;
 }
?>
<!-- categories //-->
	  <tr>
		<td>
<?php


 $info_box_contents = array();
 $info_box_contents[] = array('text' => BOX_HEADING_CATEGORIES);

 new infoBoxHeading($info_box_contents, false, false);

 $cat_pdown = array(array('id' => '', 'text' => PULL_DOWN_DEFAULT));
 $cat_array = tep_get_paths();
 unset($cat_array[0]);
 if (SHOW_COUNTS == 'true') {
foreach ($cat_array as $cat) {
  $parent = $cat['parent'];
  while ($parent > 0 ) { // add category product count to parent category product counts
	$cat_array[$parent]['prod_count'] += $cat['prod_count'];
	$parent = $cat_array[$parent]['parent'];
  }
}
 }
 if (tep_not_null($cPath)) {
$shown_path = $cPath_array;
$shown_path[] = 0;
 } else {
$shown_path = array(0);
 }
 $count = '';
 $categories_string = '';

 foreach ($cat_array as $id => $cat) {
if (SHOW_COUNTS == 'true') {
  $count = ' (' . $cat['prod_count'] . ')';
}
$cat_pdown[] = array('id' => $cat['path'], 'text' => $cat['indent'] . $cat['name'] . $count);
if (in_array($cat['parent'], $shown_path)) {
  $categories_string .= $cat['indent'] . '<a href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $cat['path']) . '">';
  if ($cat['has_subcat']) {
	$categories_string .= tep_image(DIR_WS_IMAGES . 'pointer_green.gif', '');
  } else {
	$categories_string .= tep_image(DIR_WS_IMAGES . 'pointer_green_light.gif', '');
  }
  if (isset($cPath_array) && in_array($id, $cPath_array)) {
	$categories_string .= '<b>';
  }
  if ($id == $current_category_id) {
	$categories_string .= '<span class="errorText">';
  }
  $categories_string .= $cat['name']; // display category name
  if ($id == $current_category_id) {
$categories_string .= '</span>';
  }
  if (isset($cPath_array) && in_array($id, $cPath_array)) {
	$categories_string .= '</b>';
  }
  $categories_string .= '</a>' . $count . "<br>\n";
}
 }

 $info_box_contents = array();
 $info_box_contents[] = array('text' => $categories_string);
 new infoBox($info_box_contents);
?>

		</td>
	  </tr>
<!-- categories_eof //-->

 

This is the code of the contribution. As far as I know the line that checks if there are products in the category must be like this:

 


  $products_in_category = $cat['prod_count'];
  if($products_in_category > 0){
}

But when I insert it on line 86 the category box does not generate any content. I would appreciate any help. Thank you :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...