zencart分类树统计商品数量

2012-07-10 11:39 来源:www.chinab4c.com 作者:ecshop专家

   zencart分类树统计商品数量,这个是十分实用的功能。我们在读取zencart分类树的时候,可以直接通过分类和分类之间的关系。去取得该分类下循环的商品数量。当然了。我们必须知道分类所属于的每个级别的ID以及上下级关系。

   1:includes/class/category_tree.php首先分类树数组需要先取得该数据。

    $this->tree[$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'],
      'parent' => $categories->fields['parent_id'],'categories_id' => $categories->fields['categories_id'],
      'level' => 0,
      'path' => $categories->fields['categories_id'],
      'image' => $categories->fields['categories_image'],
      'next_id' => false);

   2:function zen_show_category($counter,$ii) 函数记录zencart分类ID

     $this->box_categories_array[$ii]['categories_id'] = $this->tree[$counter]['categories_id'];

   3:增加统计分类下商品数量的函数.

    function zen_products_in_category_count($categories_id, $include_deactivated = false, $include_child = true, $limit = false) {
    global $db;
    $products_count = 0;

    if ($limit) {
      $limit_count = ' limit 1';
    } else {
      $limit_count = '';
    }

    if ($include_deactivated) {

      $products = $db->Execute("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)$categories_id . "'" . $limit_count);
    } else {
      $products = $db->Execute("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_id . "'" . $limit_count);

    }

    $products_count += $products->fields['total'];

    if ($include_child) {
      $childs = $db->Execute("select categories_id from " . TABLE_CATEGORIES . "
                              where parent_id = '" . (int)$categories_id . "'");
      if ($childs->RecordCount() > 0 ) {
        while (!$childs->EOF) {
          $products_count += zen_products_in_category_count($childs->fields['categories_id'], $include_deactivated);
          $childs->MoveNext();
        }
      }
    }
    return $products_count;
  }
 

  4:includes/templates/template_default/sideboxes/tpl_categorires.php增加以下代码

   $c = zen_products_in_category_count($box_categories_array[$i]['categories_id']);

   来源:http://www.chinab4c.com