笔者在做ecshop二次开发时曾开发过商品内容页左侧浏览过此商品的人还购买了的功能(发现京东、凡客均有此功能),在此和大家分享一下:
一、数据库设计 建一张表,ecs_goods_visite
笔者在做ecshop二次开发时曾开发过商品内容页左侧“浏览过此商品的人还购买了”的功能(发现京东、凡客均有此功能),在此和大家分享一下:
一、数据库设计
建一张表,ecs_goods_visiter,用于将记录浏览过某商品的用户。结构如下:
五个字段名分别为:表自增ID、用户ID、商品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文件,代码如下:
{$lang.view_and_shopping}
{$bought_goods_data.formated_promote_price}
{$bought_goods_data.shop_price}
最后在goods.dwt找到:
在该行下面添加:
最后效果图如下: