Ecshop商品详细页自动关联相关商品

作者:stoat 发布时间:2014-04-03 分类:闲扯

Ecshop自动关联相关商品原理:先判断后台是否人工进行关联商品,若无,则进行自动关联。自动关联相关商品原则:同分类下相关商品前4后4,共输出8个相关商品(数量可自定义,分类产品不够时输出不了8个产品)。

Ecshop自动关联相关商品修改代码1:文件goods.php,105行方法引用修改为


$linked_goods = get_linked_goods($goods_id,$goods['cat_id']);

Ecshop自动关联相关商品修改代码2:文件goods.php,替换方法get_linked_goods

function get_linked_goods($goods_id, $cat_id = 0) {
    $sql = 'SELECT g.goods_id, g.goods_name, g.goods_thumb, g.goods_img, g.shop_price AS org_price, '."IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".'g.market_price, g.promote_price, g.promote_start_date, g.promote_end_date '.'FROM '.$GLOBALS['ecs'] - >table('link_goods').' lg '.'LEFT JOIN '.$GLOBALS['ecs'] - >table('goods').' AS g ON g.goods_id = lg.link_goods_id '."LEFT JOIN ".$GLOBALS['ecs'] - >table('member_price')." AS mp "."ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' "."WHERE lg.goods_id = '$goods_id' AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 "."LIMIT ".$GLOBALS['_CFG']['related_goods_number'];
    $res = $GLOBALS['db'] - >query($sql);

    if ($GLOBALS['db'] - >num_rows($res) == 0 && $cat_id != 0) {

        $sql = "(SELECT goods_id, goods_name, goods_thumb, goods_img, shop_price FROM ".$GLOBALS['ecs'] - >table('goods')." WHERE cat_id= ".$cat_id." AND  goods_id < ".$goods_id." AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 ORDER BY goods_id DESC LIMIT 4)
UNION
(SELECT goods_id, goods_name, goods_thumb, goods_img, shop_price FROM ".$GLOBALS['ecs'] - >table('goods')." WHERE cat_id= ".$cat_id." AND  goods_id > ".$goods_id."  AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 LIMIT 4)";

        $res = $GLOBALS['db'] - >query($sql);

    }
    $arr = array();
    while ($row = $GLOBALS['db'] - >fetchRow($res)) {
        $arr[$row['goods_id']]['goods_id'] = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name'] = $row['goods_name'];
        $arr[$row['goods_id']]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        $arr[$row['goods_id']]['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['market_price'] = price_format($row['market_price']);
        $arr[$row['goods_id']]['shop_price'] = price_format($row['shop_price']);
        $arr[$row['goods_id']]['url'] = build_uri('goods', array('gid' = >$row['goods_id']), $row['goods_name']);

        if ($row['promote_price'] > 0) {
            $arr[$row['goods_id']]['promote_price'] = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
            $arr[$row['goods_id']]['formated_promote_price'] = price_format($arr[$row['goods_id']]['promote_price']);
        } else {

            $arr[$row['goods_id']]['promote_price'] = 0;
        }
    }

    return $arr;
}

修改完成后,后台刷新缓存即可。

原文地址:Ecshop商品详细页自动关联相关商品 by 雪鼬博客

标签:ecshop

评论已关闭