当前位置:首页 > 行业动态 > 正文

如何在Ecshop教程中为销售排行榜添加显示销售商品件数的功能?

要在ECShop教程中添加销售排行榜上销售出的商品件数,你需要编辑相关PHP文件。找到负责显示排行榜的代码部分,通常是在 goods_list.php或类似的文件中。在适当的位置添加统计商品销售数量的代码,例如通过查询数据库中的销售记录来累计每个商品的销售件数,然后在排行榜中显示这些数据。确保你的修改不会影响其他功能,并且在每次商品销售后更新销售数量。

ecshop教程:销售排行榜添加上销售出商品的件数

ecshop中,销售排行榜是一个重要的功能,它可以展示哪些商品最受欢迎,默认情况下,销售排行榜只显示了销售金额,但有时我们可能还需要知道具体销售的件数,小编将指导你如何在ecshop的销售排行榜上添加销售出商品的件数。

第一步:找到相关文件

你需要找到控制销售排行榜显示的文件,通常这个文件位于admin目录下的sales_list.php,使用FTP工具或者直接通过网站控制面板进入你的ecshop安装目录,找到对应的文件。

第二步:编辑PHP文件

打开sales_list.php文件,并搜索以下代码段:

$sql = 'SELECT o.order_id, o.user_id, o.extension_code, '.
       '(os.goods_name) AS goods_name, (g.brand_name) AS brand_name, '.
       'o.money_paid AS order_amount, g.goods_thumb, g.goods_img, g.original_img '.
       'FROM ' . $GLOBALS['ecs']>table('order_info') . " AS o 
        LEFT JOIN " . $GLOBALS['ecs']>table('order_goods') . ' AS og ON o.order_id=og.order_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('goods') . ' AS g ON g.goods_id = og.goods_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('goods_activity') . ' AS ga ON g.goods_id = ga.goods_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('goods_ext') . " AS ge ON g.goods_id = ge.goods_id 
        LEFT JOIN " . $GLOBALS['ecs']>table('goods_attr') . ' AS gatr ON g.goods_id=gatr.goods_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('attribute') . ' AS a ON a.attr_id = gatr.attr_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('brand') . ' AS b ON b.brand_id = g.brand_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('order_action') . ' AS oa ON o.order_sn = oa.order_sn 
        LEFT JOIN ' . $GLOBALS['ecs']>table('return_goods') . ' AS rg ON o.order_id = rg.order_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('region_warehouse') . ' AS rw ON g.regional_distribution = rw.region_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('warehouse_area_goods') . ' AS wag ON g.goods_id = wag.goods_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('warehouse_area') . ' AS wa ON wa.area_id = wag.area_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('goods_sales_list') . ' AS os ON os.goods_id = g.goods_id 
        WHERE o.order_status = 2 AND o.shipping_status = 1 '.
       "AND o.add_time >= '$start_date' AND o.add_time <= '$end_date' $filter";

第三步:修改SQL查询语句

为了获取每个订单中的商品件数,我们需要对SQL查询做一点小修改,在上述代码段的SELECT子句中,加入一个新的字段SUM(og.goods_number) AS order_goods_number来计算每个订单中的商品总件数。

修改后的查询语句如下:

$sql = 'SELECT o.order_id, o.user_id, o.extension_code, '.
       '(os.goods_name) AS goods_name, (g.brand_name) AS brand_name, '.
       'SUM(og.goods_number) AS order_goods_number, o.money_paid AS order_amount, g.goods_thumb, g.goods_img, g.original_img '.
       'FROM ' . $GLOBALS['ecs']>table('order_info') . " AS o 
        LEFT JOIN " . $GLOBALS['ecs']>table('order_goods') . ' AS og ON o.order_id=og.order_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('goods') . ' AS g ON g.goods_id = og.goods_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('goods_activity') . ' AS ga ON g.goods_id = ga.goods_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('goods_ext') . " AS ge ON g.goods_id = ge.goods_id 
        LEFT JOIN " . $GLOBALS['ecs']>table('goods_attr') . ' AS gatr ON g.goods_id=gatr.goods_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('attribute') . ' AS a ON a.attr_id = gatr.attr_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('brand') . ' AS b ON b.brand_id = g.brand_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('order_action') . ' AS oa ON o.order_sn = oa.order_sn 
        LEFT JOIN ' . $GLOBALS['ecs']>table('return_goods') . ' AS rg ON o.order_id = rg.order_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('region_warehouse') . ' AS rw ON g.regional_distribution = rw.region_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('warehouse_area_goods') . ' AS wag ON g.goods_id = wag.goods_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('warehouse_area') . ' AS wa ON wa.area_id = wag.area_id 
        LEFT JOIN ' . $GLOBALS['ecs']>table('goods_sales_list') . ' AS os ON os.goods_id = g.goods_id 
        WHERE o.order_status = 2 AND o.shipping_status = 1 '.
       "AND o.add_time >= '$start_date' AND o.add_time <= '$end_date' $filter";

第四步:更新数据展示

您需要在展示数据的HTML部分添加新的列来显示商品件数,这通常在sales_list.php文件中的循环里进行,找到类似下面的代码:

' <td>'.$value['order_amount'].'</td>';

在该行之前添加:

' <td>'.$value['order_goods_number'].'</td>';

完成

保存更改后,重新加载销售排行榜页面,你应该能看到每项商品旁边的销售件数了。

相关问题与解答

Q1: 如果我想按照销售件数而不是销售额来排序销售排行榜,我应该怎么修改?

A1: 要实现这个功能,你需要修改sales_list.php中的SQL查询语句的ORDER BY部分,默认情况下,它可能类似于这样:

$sql .= " ORDER BY order_amount DESC";

将其更改为:

$sql .= " ORDER BY order_goods_number DESC";

这将使销售排行榜根据销售的件数从高到低进行排序。

Q2: 我可以在销售排行榜中同时显示销售件数和销售额吗?

A2: 当然可以,在修改SQL查询以包含销售件数之后,你已经在结果集中有了这两个值,只需在展示数据的HTML表格中增加一个额外的列来显示销售额即可,你可以在现有代码中找到类似下面的代码:

' <td>'.$value['order_goods_number'].'</td>';

在这行代码之后添加:

' <td>'.$value['order_amount'].'</td>';

销售排行榜将同时显示每项商品的销售件数和销售额。

0