笔者在做ecshop二次开发时曾开发过商品内容页左侧浏览过此商品的人还购买了的功能(发现京东、凡客均有此功能),在此和大家分享一下: 一、数据库设计 建一张表,ecs_goods_visite
笔者在做ecshop二次开发时曾开发过商品内容页左侧“浏览过此商品的人还购买了”的功能(发现京东、凡客均有此功能),在此和大家分享一下:
一、数据库设计建一张表,ecs_goods_visiter,用于将记录浏览过某商品的用户。结构如下:
![](https://img-my.csdn.net/uploads/201206/30/1341048586_1160.jpg)
五个字段名分别为:表自增ID、用户ID、商品ID、浏览时间、浏览次数。同一用户浏览同一商品时更改浏览时间即可。
二、修改goods.php文件
在文件末尾添加函数如:
function get_view_and_bought($goods_id) {
二、修改goods.php文件
在文件末尾添加函数如:
function get_view_and_bought($goods_id) {
$sql = 'SELECT d.goods_id, d.goods_name, d.goods_thumb, d.goods_img, d.shop_price, d.promote_price, d.promote_start_date, d.promote_end_date ' .
'FROM ' . $GLOBALS['ecs']->table('goods_visitor') . ' AS v ' .
'LEFT JOIN ' . $GLOBALS['ecs']->table('order_info') . ' AS b ON b.user_id = v.user_id ' .
'LEFT JOIN ' . $GLOBALS['ecs']->table('order_goods') . ' AS c ON c.order_id = b.order_id ' .
'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS d ON d.goods_id = c.goods_id ' .
"WHERE v.goods_id = '$goods_id' AND (b.shipping_status = 1 OR b.shipping_status = 2 OR b.shipping_status = 5) AND d.is_on_sale = 1 AND d.is_alone_sale = 1 AND d.is_delete = 0 " .
'ORDER BY b.order_id DESC ' .
'LIMIT ' . $GLOBALS['_CFG']['bought_goods'];
$res = $GLOBALS['db']->query($sql);
$key = 0;
$arr = array();
while ($row = $GLOBALS['db']->fetchRow($res)){
$arr[$key]['goods_id'] = $row['goods_id'];
$arr[$key]['goods_name'] = $row['goods_name'];
$arr[$key]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
$arr[$key]['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$arr[$key]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
$arr[$key]['shop_price'] = price_format($row['shop_price']);
$arr[$key]['url'] = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
if ($row['promote_price'] > 0) {
$arr[$key]['promote_price'] = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
$arr[$key]['formated_promote_price'] = price_format($arr[$key]['promote_price']);
}else {
$arr[$key]['promote_price'] = 0;
}
$key++;
}
return $arr;
}
最后找到:$smarty->assign('bought_goods', get_also_bought($goods_id)); // 购买了该商品的用户还购买了哪些商品
并在该行下面添加:
$smarty->assign('view_and_bought', get_view_and_bought($goods_id)); // 浏览过该商品的用户还购买了哪些商品
三、修改模板文件goods.dwt
在library文件夹中新建view_and_shopping.lbi文件,代码如下:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
在该行下面添加:<!-- #BeginLibraryItem "/library/view_and_shopping.lbi" --><!-- #EndLibraryItem -->
最后效果图如下:
最后找到:$smarty->assign('bought_goods', get_also_bought($goods_id)); // 购买了该商品的用户还购买了哪些商品
并在该行下面添加:
$smarty->assign('view_and_bought', get_view_and_bought($goods_id)); // 浏览过该商品的用户还购买了哪些商品
三、修改模板文件goods.dwt
在library文件夹中新建view_and_shopping.lbi文件,代码如下:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- {if $view_and_bought} -->
<div class="box">
<div class="box_1" id="left_box_re">
<h3><span class="text">{$lang.view_and_shopping}</span></h3>
<div class="boxCenterList clearfix ie6" style="margin:0;padding-left:7px;">
<!--{foreach from=$view_and_bought item=bought_goods_data}-->
<ul class="clearfix">
<li class="goodsimg"><a href="{$bought_goods_data.url}"><img src="{$bought_goods_data.goods_thumb}" alt="{$bought_goods_data.goods_name}" class="B_blue" /></a></li>
<li><a href="{$bought_goods_data.url}" title="{$bought_goods_data.goods_name}">{$bought_goods_data.short_name}</a>
<!-- {if $bought_goods_data.promote_price neq 0} -->
<font class="shop_s">{$bought_goods_data.formated_promote_price}</font>
<!-- {else} -->
<font class="shop_s">{$bought_goods_data.shop_price}</font>
<!-- {/if} -->
</li>
</ul>
<!-- {/foreach} -->
</div>
</div>
</div>
<div class="blank5"></div>
<!-- {/if} -->
最后在goods.dwt找到:<!-- #BeginLibraryItem "/library/history.lbi" --><!-- #EndLibraryItem -->
在该行下面添加:<!-- #BeginLibraryItem "/library/view_and_shopping.lbi" --><!-- #EndLibraryItem -->
![](https://img-my.csdn.net/uploads/201206/30/1341048604_6251.jpg)
http://www.4vyuan.com/a/course/comsys/2012/0403/197.html