一、首先找到lib_goods.php文件,添加以下代码:
/**
* 调用产品分类的销售排行榜
*
* @access public
* @param string $cats 查询的分类
* @return array
*/
function get_cats_top10($cat = '')
{
$sql = 'SELECT cat_id, cat_name ' .
'FROM ' . $GLOBALS['ecs']->table('category') .
"WHERE parent_id = '$cat' ORDER BY sort_order ASC, cat_id ASC LIMIT 3";
$res = $GLOBALS['db']->getAll($sql);
foreach ($res AS $row)
{
$cats = get_children($row['cat_id']);
$cat_arr[$row['cat_id']]['name'] = mb_substr($row['cat_name'] , 0, 10 ,"gbk");
$cat_arr[$row['cat_id']]['id'] = $row['cat_id'];
$where = !empty($cats) ? "AND ($cats) " : '';
switch ($GLOBALS['_CFG']['top10_time'])
{
case 1: // 一年
$top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 365 * 86400) . "'";
break;
case 2: // 半年
$top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 180 * 86400) . "'";
break;
case 3: // 三个月
$top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 90 * 86400) . "'";
break;
case 4: // 一个月
$top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 30 * 86400) . "'";
break;
default:
$top10_time = '';
}
$sql = 'SELECT g.goods_id, g.goods_name, g.goods_img, g.goods_thumb, g.shop_price, g.promote_price, g.promote_start_date, g.promote_end_date, 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 $where $top10_time " ;
//判断是否启用库存,库存数量是否大于0
if ($GLOBALS['_CFG']['use_storage'] == 1)
{
$sql .= " AND g.goods_number > 0 ";
}
$sql .= ' AND og.order_id = o.order_id AND og.goods_id = g.goods_id ' .
"AND o.order_status = '" . OS_CONFIRMED . "' " .
"AND (o.pay_status = '" . PS_PAYED . "' OR o.pay_status = '" . PS_PAYING . "') " .
"AND (o.shipping_status = '" . SS_SHIPPED . "' OR o.shipping_status = '" . SS_RECEIVED . "') " .
'GROUP BY g.goods_id ORDER BY goods_number DESC, g.goods_id DESC LIMIT ' . $GLOBALS['_CFG']['top_number'];
$arr = $GLOBALS['db']->getAll($sql);
foreach ($arr as $row1)
{
$cat_arr[$row['cat_id']]['children'][$row1['goods_id']]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row1['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row1['goods_name'];
$cat_arr[$row['cat_id']]['children'][$row1['goods_id']]['url'] = build_uri('goods', array('gid' => $row1['goods_id']), $row1['goods_name']);
$cat_arr[$row['cat_id']]['children'][$row1['goods_id']]['goods_img'] = get_image_path($row1['goods_id'], $row1['goods_img']);
$cat_arr[$row['cat_id']]['children'][$row1['goods_id']]['goods_thumb'] = get_image_path($row1['goods_id'], $row1['goods_thumb']);
$cat_arr[$row['cat_id']]['children'][$row1['goods_id']]['shop_price'] = price_format($row1['shop_price']);
if ($row1['promote_price'] > 0)
{
$promote_price = bargain_price($row1['promote_price'], $row1['promote_start_date'], $row1['promote_end_date']);
$cat_arr[$row['cat_id']]['children'][$row1['goods_id']]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
}
else
{
$cat_arr[$row['cat_id']]['children'][$row1['goods_id']]['promote_price'] = '';
}
}
}
return $cat_arr;
}
二、找到index.php文件
在
$smarty->assign('top_goods', get_top10()); // 销售排行
下添加代码:
$smarty->assign('top_goods_add', get_cats_top10(0)); // 指定分类下销售排行,0代表顶级分类,可以换成指定id
三、模板调用,可新建一个模板,具体想要什么样式,可自行修改:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<div class="box">
<div class="box_2">
<h3><span>销售排行榜</span></h3>
<div class="top10List clearfix">
<!-- {foreach name=top from=$top_goods_add item=goods}-->
<!-- {foreach from=$goods.children item=children name=top_goods}-->
<ul class="clearfix">
<img src="../images/top_{$smarty.foreach.top_goods.iteration}.gif" class="iteration" />
<!-- {if $smarty.foreach.top_goods.iteration<4}-->
<li class="topimg">
<a href="{$children.url}"><img src="{$children.goods_thumb}" alt="{$children.name|escape:html}" class="samllimg" /></a>
</li>
<!-- {/if} -->
<li {if $smarty.foreach.top_goods.iteration<4}class="iteration1"{/if}>
<a href="{$children.url}" title="{$children.name|escape:html}">{$children.short_name}</a><br />
{$lang.shop_price}<font class="f1">{$children.price}</font><br />
</li>
</ul>
<!--{/foreach}-->
<!--{/foreach}-->
</div>
</div>
</div>
<div class="blank5"></div>