Ecshop修改后台的大分类商品数量
修改后效果:
解决方法:
1.修改ecshop/includes/lib_common.php文件代码。
添加计算分类下的商品总数方法:
/**
*
* 使用递归,算出分类下的子类goods_num
* @param 分类id $cat_id
*/
function get_sub_goods_number($cat_id){
$sub_goods_num=0;
//算出某类的子分类
$sql="select cat_id from ".$GLOBALS['ecs']->table('category') ." where parent_id=".$cat_id;
$res5 = $GLOBALS['db']->getAll($sql);
if($res5==null){
return 0;
}
foreach($res5as$k1=>$v1){
//子分类对应的
$sql="select count(*) as goods_num from ".$GLOBALS['ecs']->table('goods') ." where cat_id=".$v1['cat_id']." and is_delete = 0 AND is_on_sale = 1";
//print($sql);
$res6 = $GLOBALS['db']->getAll($sql);
$goods_num=0;
foreach($res6as$k6=>$v6){
$goods_num=$v6['goods_num'];
}
$sub_goods_num=$sub_goods_num+$goods_num+get_sub_goods_number($v1['cat_id']);
}
return $sub_goods_num;
}
2.修改ecshop/includes/lib_common.php文件的cat_list()方法
修改$res2的sql语句
//计算每个分类下的商品数,不包括子分类
$sql="select b.cat_id,(case when a.cat_id<>'null' then count(*) else '0' end) as goods_num from ".$GLOBALS['ecs']->table('goods'). " as a right join ".$GLOBALS['ecs']->table('category'). " as b on a.cat_id=b.cat_id group by b.cat_id";
加了下划线的代码为修改后的代码
foreach($res2as$k=>$v)
{
//1.得到所有分类id
//2.根据这些id查询相应的goods_number
$sql="select count(*) as goods_num from ".$GLOBALS['ecs']->table('goods'). " where cat_id=".$v['cat_id']. " AND is_delete = 0 AND is_on_sale = 1";
$res4 = $GLOBALS['db']->getAll($sql);
$goods_num=0;
foreach($res4 as $k4=>$v4){
$goods_num=$v4['goods_num'];
}
$v['goods_num']=$goods_num;
$newres[$v['cat_id']] = $v['goods_num']+get_sub_goods_number($v['cat_id']);
foreach($res3as$ks=>$vs)
{
if($v['cat_id'] == $vs['cat_id'])
{
$newres[$v['cat_id']] = $v['goods_num'] + $vs['goods_num'];
}
}
}