转自:http://www.phpally.com/ecshop%E8%87%AA%E5%AE%9A%E4%B9%89%E9%94%80%E9%87%8F/
本补丁以假乱真,对网站销售有一定帮助,计算公式:前台显示销量 = 后台自定义销量 = 实际销量,以下讲解以官方默认模板(default)为例
后台商品列表
前台效果
开发指南
一、将安装文件覆盖到网站根目录(覆盖前注意备份),输入”站点路径/db_patch.php”,如”http://ecshop.phpally.com/db_patch.php”一键完成数据库补丁的安装,安装完成后,请立即删除db_patch.php。
1 <?php 2 /** 3 * ECSHOP 自定义销量 数据库补丁 4 * ---------------------------------------------------------------------------- 5 * http://www.phpally.com 6 * Jacklee的博客 致力于php技术 7 * ---------------------------------------------------------------------------- 8 * 作者: Jacklee 9 * 邮箱: 349392900#qq.com 10 * 创建时间: 2013-09-27 11 * 最后修改时间: 2013-09-27 12 */ 13 14 define('IN_ECS', true); 15 require(dirname(__FILE__) . '/includes/init.php'); 16 17 $sql = " 18 ALTER TABLE `{$prefix}goods` ADD `sales_volume_base` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0' COMMENT '销量基数' 19 "; 20 $db->query($sql); 21 echo '数据库补丁程序执行成功,请立即删除此补丁文件(db_patch.php)。';
二、编辑/admin/goods.php,添加修改销量基数(sales_volume_base)
1 *------------------------------------------------------ */ 2 //-- 修改商品销量基数 3 /*------------------------------------------------------ */ 4 elseif ($_REQUEST['act'] == 'edit_sales_volume_base') 5 { 6 check_authz_json('goods_manage'); 7 8 $goods_id = intval($_POST['id']); 9 $sales_volume_base = json_str_iconv(trim($_POST['val'])); 10 11 if ($exc->edit("sales_volume_base = '$sales_volume_base', last_update=" .gmtime(), $goods_id)) 12 { 13 clear_cache_files(); 14 make_json_result(stripslashes($sales_volume_base)); 15 } 16 }
三、编辑/admin/includes/lib_goods.php,添加查询销量基数(sales_volume_base)
1 ql = "SELECT goods_id, goods_name, goods_type, goods_sn, shop_price, is_on_sale, is_best, is_new, is_hot, sort_order, goods_number, integral, sales_volume_base, " . 2 " (promote_price > 0 AND promote_start_date <= '$today' AND promote_end_date >= '$today') AS is_promote ". 3 " FROM " . $GLOBALS['ecs']->table('goods') . " AS g WHERE is_delete='$is_delete' $where" . 4 " ORDER BY $filter[sort_by] $filter[sort_order] ". 5 " LIMIT " . $filter['start'] . ",$filter[page_size]";
四、编辑/admin/templates/goods_list.htm,添加显示销量基数(sales_volume_base)
1 <table cellpadding="3" cellspacing="1"> 2 <tr> 3 <th> 4 <input onclick='listTable.selectAll(this, "checkboxes")' type="checkbox" /> 5 <a href="javascript:listTable.sort('goods_id'); ">{$lang.record_id}</a>{$sort_goods_id} 6 </th> 7 <th><a href="javascript:listTable.sort('goods_name'); ">{$lang.goods_name}</a>{$sort_goods_name}</th> 8 <th><a href="javascript:listTable.sort('goods_sn'); ">{$lang.goods_sn}</a>{$sort_goods_sn}</th> 9 <th><a href="javascript:listTable.sort('shop_price'); ">{$lang.shop_price}</a>{$sort_shop_price}</th> 10 <th><a href="javascript:listTable.sort('is_on_sale'); ">{$lang.is_on_sale}</a>{$sort_is_on_sale}</th> 11 <th><a href="javascript:listTable.sort('is_best'); ">{$lang.is_best}</a>{$sort_is_best}</th> 12 <th><a href="javascript:listTable.sort('is_new'); ">{$lang.is_new}</a>{$sort_is_new}</th> 13 <th><a href="javascript:listTable.sort('is_hot'); ">{$lang.is_hot}</a>{$sort_is_hot}</th> 14 <th><a href="javascript:listTable.sort('sort_order'); ">{$lang.sort_order}</a>{$sort_sort_order}</th> 15 {if $use_storage} 16 <th><a href="javascript:listTable.sort('goods_number'); ">{$lang.goods_number}</a>{$sort_goods_number}</th> 17 {/if} 18 <th><a href="javascript:listTable.sort('sales_volume_base'); ">{$lang.sales_volume_base}</a>{$sort_sales_volume_base}</th> 19 <th>{$lang.handler}</th> 20 <tr> 21 {foreach from=$goods_list item=goods} 22 <tr> 23 <td><input type="checkbox" name="checkboxes[]" value="{$goods.goods_id}" />{$goods.goods_id}</td> 24 <td class="first-cell" style="{if $goods.is_promote}color:red;{/if}"><span onclick="listTable.edit(this, 'edit_goods_name', {$goods.goods_id})">{$goods.goods_name|escape:html}</span></td> 25 <td><span onclick="listTable.edit(this, 'edit_goods_sn', {$goods.goods_id})">{$goods.goods_sn}</span></td> 26 <td align="right"><span onclick="listTable.edit(this, 'edit_goods_price', {$goods.goods_id})">{$goods.shop_price} 27 28 </span></td> 29 <td align="center"><img src="images/{if $goods.is_on_sale}yes{else}no{/if}.gif" onclick="listTable.toggle(this, 'toggle_on_sale', {$goods.goods_id})" /></td> 30 <td align="center"><img src="images/{if $goods.is_best}yes{else}no{/if}.gif" onclick="listTable.toggle(this, 'toggle_best', {$goods.goods_id})" /></td> 31 <td align="center"><img src="images/{if $goods.is_new}yes{else}no{/if}.gif" onclick="listTable.toggle(this, 'toggle_new', {$goods.goods_id})" /></td> 32 <td align="center"><img src="images/{if $goods.is_hot}yes{else}no{/if}.gif" onclick="listTable.toggle(this, 'toggle_hot', {$goods.goods_id})" /></td> 33 <td align="center"><span onclick="listTable.edit(this, 'edit_sort_order', {$goods.goods_id})">{$goods.sort_order}</span></td> 34 {if $use_storage} 35 <td align="right"><span onclick="listTable.edit(this, 'edit_goods_number', {$goods.goods_id})">{$goods.goods_number}</span></td> 36 {/if} 37 <td align="center"><span onclick="listTable.edit(this, 'edit_sales_volume_base', {$goods.goods_id})">{$goods.sales_volume_base}</span></td> 38 <td align="center"> 39 <a href="../goods.php?id={$goods.goods_id}" target="_blank" title="{$lang.view}"><img src="images/icon_view.gif" width="16" height="16" border="0" /></a> 40 <a href="goods.php?act=edit&goods_id={$goods.goods_id}{if $code neq 'real_goods'}&extension_code={$code}{/if}" title="{$lang.edit}"><img src="images/icon_edit.gif" width="16" height="16" border="0" /></a> 41 <a href="goods.php?act=copy&goods_id={$goods.goods_id}{if $code neq 'real_goods'}&extension_code={$code}{/if}" title="{$lang.copy}"><img src="images/icon_copy.gif" width="16" height="16" border="0" /></a> 42 <a href="javascript:;" onclick="listTable.remove({$goods.goods_id}, '{$lang.trash_goods_confirm}')" title="{$lang.trash}"><img src="images/icon_trash.gif" width="16" height="16" border="0" /></a> 43 {if $specifications[$goods.goods_type] neq ''}<a href="goods.php?act=product_list&goods_id={$goods.goods_id}" title="{$lang.item_list}"><img src="images/icon_docs.gif" width="16" height="16" border="0" /></a>{else}<img src="images/empty.gif" width="16" height="16" border="0" />{/if} 44 {if $add_handler} 45 | 46 {foreach from=$add_handler item=handler} 47 <a href="{$handler.url}&goods_id={$goods.goods_id}" title="{$handler.title}"><img src="images/{$handler.img}" width="16" height="16" border="0" /></a> 48 {/foreach} 49 {/if} 50 </td> 51 </tr> 52 {foreachelse} 53 <tr><td class="no-records" colspan="10">{$lang.no_records}</td></tr> 54 {/foreach} 55 </table>
五、编辑/languages/zh_cn/admin/goods.php,添加语言项
1 2 $_LANG['sales_volume_base'] = '销量基数';
六、编辑/includes/lib_goods.php,修改get_goods_info函数,计算累计销量(sales_volume_total)
1 /** 2 * 获得商品的详细信息 3 * 4 * @access public 5 * @param integer $goods_id 6 * @return void 7 */ 8 function get_goods_info($goods_id) 9 { 10 $time = gmtime(); 11 $sql = 'SELECT g.*, c.measure_unit, b.brand_id, b.brand_name AS goods_brand, m.type_money AS bonus_money, ' . 12 'IFNULL(AVG(r.comment_rank), 0) AS comment_rank, ' . 13 "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS rank_price " . 14 'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . 15 'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS c ON g.cat_id = c.cat_id ' . 16 'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON g.brand_id = b.brand_id ' . 17 'LEFT JOIN ' . $GLOBALS['ecs']->table('comment') . ' AS r '. 18 'ON r.id_value = g.goods_id AND comment_type = 0 AND r.parent_id = 0 AND r.status = 1 ' . 19 'LEFT JOIN ' . $GLOBALS['ecs']->table('bonus_type') . ' AS m ' . 20 "ON g.bonus_type_id = m.type_id AND m.send_start_date <= '$time' AND m.send_end_date >= '$time'" . 21 " LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ". 22 "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ". 23 "WHERE g.goods_id = '$goods_id' AND g.is_delete = 0 " . 24 "GROUP BY g.goods_id"; 25 $row = $GLOBALS['db']->getRow($sql); 26 27 /* 查询该商品的实际销量 */ 28 $sql = 'SELECT IFNULL(SUM(g.goods_number), 0) ' . 29 'FROM ' . $GLOBALS['ecs']->table('order_info') . ' AS o, ' . 30 $GLOBALS['ecs']->table('order_goods') . ' AS g ' . 31 "WHERE o.order_id = g.order_id " . 32 "AND o.order_status = '" . OS_CONFIRMED . "' " . 33 "AND o.shipping_status " . db_create_in(array(SS_SHIPPED, SS_RECEIVED)) . 34 " AND o.pay_status " . db_create_in(array(PS_PAYED, PS_PAYING)) . 35 " AND g.goods_id = '$goods_id'" ; 36 $sales_count = $GLOBALS['db']->getOne($sql); 37 38 /* 累计销量 = 自定义销量基数 + 实际销量 */ 39 $row['sales_volume_total'] = $row['sales_volume_base'] + $sales_count; 40 41 if ($row !== false) 42 { 43 /* 用户评论级别取整 */ 44 $row['comment_rank'] = ceil($row['comment_rank']) == 0 ? 5 : ceil($row['comment_rank']); 45 46 /* 获得商品的销售价格 */ 47 $row['market_price'] = price_format($row['market_price']); 48 $row['shop_price_formated'] = price_format($row['shop_price']); 49 50 /* 修正促销价格 */ 51 if ($row['promote_price'] > 0) 52 { 53 $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']); 54 } 55 else 56 { 57 $promote_price = 0; 58 } 59 60 /* 处理商品水印图片 */ 61 $watermark_img = ''; 62 63 if ($promote_price != 0) 64 { 65 $watermark_img = "watermark_promote"; 66 } 67 elseif ($row['is_new'] != 0) 68 { 69 $watermark_img = "watermark_new"; 70 } 71 elseif ($row['is_best'] != 0) 72 { 73 $watermark_img = "watermark_best"; 74 } 75 elseif ($row['is_hot'] != 0) 76 { 77 $watermark_img = 'watermark_hot'; 78 } 79 80 if ($watermark_img != '') 81 { 82 $row['watermark_img'] = $watermark_img; 83 } 84 85 $row['promote_price_org'] = $promote_price; 86 $row['promote_price'] = price_format($promote_price); 87 88 /* 修正重量显示 */ 89 $row['goods_weight'] = (intval($row['goods_weight']) > 0) ? 90 $row['goods_weight'] . $GLOBALS['_LANG']['kilogram'] : 91 ($row['goods_weight'] * 1000) . $GLOBALS['_LANG']['gram']; 92 93 /* 修正上架时间显示 */ 94 $row['add_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']); 95 96 /* 促销时间倒计时 */ 97 $time = gmtime(); 98 if ($time >= $row['promote_start_date'] && $time <= $row['promote_end_date']) 99 { 100 $row['gmt_end_time'] = $row['promote_end_date']; 101 } 102 else 103 { 104 $row['gmt_end_time'] = 0; 105 } 106 107 /* 是否显示商品库存数量 */ 108 $row['goods_number'] = ($GLOBALS['_CFG']['use_storage'] == 1) ? $row['goods_number'] : ''; 109 110 /* 修正积分:转换为可使用多少积分(原来是可以使用多少钱的积分) */ 111 $row['integral'] = $GLOBALS['_CFG']['integral_scale'] ? round($row['integral'] * 100 / $GLOBALS['_CFG']['integral_scale']) : 0; 112 113 /* 修正优惠券 */ 114 $row['bonus_money'] = ($row['bonus_money'] == 0) ? 0 : price_format($row['bonus_money'], false); 115 116 /* 修正商品图片 */ 117 $row['goods_img'] = get_image_path($goods_id, $row['goods_img']); 118 $row['goods_thumb'] = get_image_path($goods_id, $row['goods_thumb'], true); 119 120 return $row; 121 } 122 else 123 { 124 return false; 125 } 126 }
七、以默认模板为例,编辑/themes/default/goods.dwt,添加显示累计销量
1 <li class="clearfix"> 2 <dd> 3 <strong>{$lang.sales_volume_total}<font color="#FE596A" size="2">{$goods.sales_volume_total}{$lang.pcs}</font></strong> 4 </dd> 5 <dd> 6 </dd> 7 </li>
八、编辑/languages/zh_cn/common.php,添加语言项
1 $_LANG['sales_volume_total'] = '累计销量:'; 2 $_LANG['pcs'] = '件';