首先,ecshop的信息显示模块在../includes/lib_goods.php文件
在其末尾添加下面这个函数
月销量:(和总销量二选一)
function ec_buysum($goods_id)
{
$LMonth=strtotime("last month"); //前一个月
$nowTime=time(); //当前时间
$sql="select sum(goods_number) from " . $GLOBALS['ecs']->table('order_goods') . " AS g ,".$GLOBALS['ecs']->table('order_info') . " AS o WHERE o.order_id=g.order_id and g.goods_id = ".$goods_id." and o.pay_status=2 and o.add_time >= ".$LMonth." and o.add_time <= ".$nowTime." group by g.goods_id";
if (($GLOBALS['db']->getOne($sql)) == ""){
return "0";
}else{
return $GLOBALS['db']->getOne($sql);
}
}
总销量:(和月销量二选一)
function ec_buysum($goods_id)
{
$sql = "select sum(goods_number) from " . $GLOBALS['ecs']->table('order_goods') . " AS g ,".$GLOBALS['ecs']->table('order_info') . " AS o WHERE o.order_id=g.order_id and g.goods_id = " . $goods_id . " and o.order_status=5 " ;
if (($GLOBALS['db']->getOne($sql)) == ""){
return "0";
}else{
return $GLOBALS['db']->getOne($sql);
}
}
然后,在上面的代码位置,大约是317行左右,找到
$goods[$idx]['brand_name'] = isset($goods_data['brand'][$row['goods_id']]) ? $goods_data['brand'][$row['goods_id']] : '';
在它的下面添加
$goods[$idx]['buy_num']= ec_buysum($row['goods_id']);
这样,你在模版中使用
$goods.buy_num
就可以调用销量数据了。
当然,这里只是修改了首页调用部分,在内页,即商品详情,你仍然只修改这个文件
在大概594行的空白位置插入
/*显示商品销量*/
$row['buy_num'] = ec_buysum($row['goods_id']);
如果你想问,在分类页如何操作呢?
分类页与这个类似,分类页文件名为
./category.php
在第508行下面插入
$row['buy_num'] = ec_buysum($row['goods_id']);
就可以了.
在模版中合适的位置使用
$goods.buy_num
来调用商品数据
=================================
或者
admin/order.php
发货的时候 ,统计总的销量
第900行
/* 方法1 更新商品销量 根据这个订单 在原来的基础增加销量*/
$sql = 'SELECT goods_id,goods_number FROM '.$GLOBALS['ecs']->table('order_goods').' WHERE order_id ='.$order_id;
$order_res = $GLOBALS['db']->getAll($sql);
foreach($order_res as $idx=>$val)
{
$sql = "update " . $ecs->table('goods') . " set sales_volume=sales_volume + '$val[goods_number]' WHERE goods_id =".$val['goods_id'];
$db->query($sql);
}
/* 方法2 更新商品销量 统计商品的每次的销量的记录 */
$sql = 'SELECT goods_id,goods_number FROM '.$GLOBALS['ecs']->table('order_goods').' WHERE order_id ='.$order_id;
$order_res = $GLOBALS['db']->getAll($sql);
foreach($order_res as $idx=>$val)
{
$sql = 'SELECT SUM(og.goods_number) as goods_number ' .
'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g, ' .
$GLOBALS['ecs']->table('order_info') . ' AS o, ' .
$GLOBALS['ecs']->table('order_goods') . ' AS og ' .
"WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND og.order_id = o.order_id AND og.goods_id = g.goods_id " .
"AND (o.order_status = '" . OS_CONFIRMED . "' OR o.order_status = '" . OS_SPLITED . "') " .
"AND (o.pay_status = '" . PS_PAYED . "' OR o.pay_status = '" . PS_PAYING . "') " .
"AND (o.shipping_status = '" . SS_SHIPPED . "' OR o.shipping_status = '" . SS_RECEIVED . "') AND g.goods_id=".$val['goods_id'];
$sales_volume = $GLOBALS['db']->getOne($sql);
$sql = "update " . $ecs->table('goods') . " set sales_volume=$sales_volume WHERE goods_id =".$val['goods_id'];
$db->query($sql);
}
=============================