这里是普通文章模块栏目内容页
ECShop:Ajax更新购物车商品数量

ECShop版本:V2.7.3 RELEASE 20110907 (V2.7.2)

涉及修改文件:

1. themes\default\flow.dwt

2. flow.php

详细修改记录:

1. themes\default\flow.dwt

第一步:搜索<!-- {foreach from=$goods_list item=goods} -->,

共有两处,这里取第一处(53行左右),

将其下一行的<tr>修改成<tr id="tr_goods_{$goods.rec_id}">

第 二步:搜索<input type="text" name="goods_number[{$goods.rec_id}]" id="goods_number_{$goods.rec_id}" value="{$goods.goods_number}" size="4" class="inputBg" style="text-align:center " onkeydown="showdiv(this)"/>

只有一处(91行左右),将其修改为

<a href="javascript:void(0)" onclick="changenum({$goods.rec_id},-1)"><img src="images/sub.png" style="vertical-align:middle;margin-bottom: 7px;" /></a>

<input type="text" name="goods_number[{$goods.rec_id}]" id="goods_number_{$goods.rec_id}" value="{$goods.goods_number}" size="4" class="inputBg" style="text-align:center " onchange="change_goods_number({$goods.rec_id},this.value)" />

<a href="javascript:void(0)" onclick="changenum({$goods.rec_id},1)"><img src="images/add.png" style="vertical-align:middle;margin-bottom: 7px;"/></a>

第三步:搜索<td align="right" bgcolor="#ffffff">{$goods.subtotal}</td>

只有一处(96行左右),将其修改为

<td align="right" bgcolor="#ffffff" id="goods_subtotal_{$goods.rec_id}">{$goods.subtotal}</td>

第四步:搜索<!-- {if $discount gt 0}-->{$your_discount}<br /><!-- {/if} -->

共两处,这里取第一处(108行左右,2.7.2版在110行左右),将其上一行

<td bgcolor="#ffffff">

修改为

<td bgcolor="#ffffff" id="total_desc">

第 五步:搜索<inputtype="button" value="{$lang.clear_cart}"class="bnt_blue_1"onclick="location.href='flow.php?step=clear'" /> (112行左右,2.7.2版在114行左右),将其删除

第六步:搜索<!-- {if $smarty.session.user_id gt 0} -->

只有一处(124行左右,2.7.2版在126行左右),在该行前面添加如下代码

        <script type="text/javascript">

            function changenum(rec_id, diff)

            {

                var goods_number =Number($$('goods_number_' + rec_id).value) + Number(diff);             

                change_goods_number(rec_id,goods_number);

            }

            function change_goods_number(rec_id, goods_number)

            {     

               Ajax.call('flow.php?step=ajax_update_cart', 'rec_id=' + rec_id +'&goods_number=' + goods_number, change_goods_number_response, 'POST','JSON');                

            }

            function change_goods_number_response(result)

            {               

                if (result.error == 0)

                {

                    var rec_id = result.rec_id;

                    $$('goods_number_' +rec_id).value = result.goods_number;//更新数量

                    $$('goods_subtotal_' +rec_id).innerHTML = result.goods_subtotal;//更新小计

                    if (result.goods_number <= 0)

                    {// 数量为零则隐藏所在行

                        $$('tr_goods_' +rec_id).style.display = 'none';

                        $$('tr_goods_' +rec_id).innerHTML = '';

                    }

                    $$('total_desc').innerHTML =result.total_desc;//更新合计

                    if ($$('ECS_CARTINFO'))

                    {//更新购物车数量

                       $$('ECS_CARTINFO').innerHTML = result.cart_info;

                    }

                }

                else if (result.message != '')

                {

                    alert(result.message);

                }                

            }

        </script>

2.flow.php

第一步:搜索elseif ($_REQUEST['step'] == 'update_cart')

只有一处(1800行左右),在其前面添加如下代码

/*------------------------------------------------------*/

//-- Ajax更新购物车add 20120118

/*------------------------------------------------------*/

elseif ($_REQUEST['step']== 'ajax_update_cart')

{

    require_once(ROOT_PATH .'includes/cls_json.php');

    $json = new JSON();

    $result = array('error' => 0, 'message'=> '');

    if (isset($_POST['rec_id']) &&isset($_POST['goods_number']))

    {

        $key = $_POST['rec_id'];

        $val = $_POST['goods_number'];

        $val = intval(make_semiangle($val));

        if ($val <= 0 &&!is_numeric($key))

        {

            $result['error'] = 99;

            $result['message'] = '';            

            die($json->encode($result));

        }

        //查询:

        $sql = "SELECT `goods_id`, `goods_attr_id`,`product_id`, `extension_code` FROM" .$GLOBALS['ecs']->table('cart').

               " WHERE rec_id='$key' AND session_id='" . SESS_ID . "'";

        $goods =$GLOBALS['db']->getRow($sql);

        $sql = "SELECT g.goods_name,g.goods_number ".

                "FROM ".$GLOBALS['ecs']->table('goods'). " AS g, ".

                   $GLOBALS['ecs']->table('cart'). " AS c ".

                "WHERE g.goods_id =c.goods_id AND c.rec_id = '$key'";

        $row = $GLOBALS['db']->getRow($sql);

        //查询:系统启用了库存,检查输入的商品数量是否有效

        if(intval($GLOBALS['_CFG']['use_storage']) > 0 &&$goods['extension_code'] != 'package_buy')

        {

            if ($row['goods_number'] < $val)

            {

                $result['error'] = 1;

                $result['message'] =sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'],$row['goods_number'], $row['goods_number']);

                die($json->encode($result));

            }

            /* 是货品*/

            $goods['product_id'] = trim($goods['product_id']);

            if (!empty($goods['product_id']))

            {

                $sql = "SELECT product_number FROM " .$GLOBALS['ecs']->table('products'). " WHERE goods_id = '" . $goods['goods_id'] . "' AND product_id = '" .$goods['product_id'] . "'";

                $product_number =$GLOBALS['db']->getOne($sql);

                if ($product_number < $val)

                {

                    $result['error'] = 2;

                    $result['message'] =sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'],

                   $product_number['product_number'], $product_number['product_number']);

                   die($json->encode($result));

                }

            }

        }

        elseif (intval($GLOBALS['_CFG']['use_storage'])> 0 && $goods['extension_code'] == 'package_buy')

        {

            if(judge_package_stock($goods['goods_id'], $val))

            {

                $result['error'] = 3;

                $result['message'] =$GLOBALS['_LANG']['package_stock_insufficiency'];

                die($json->encode($result));

            }

        }

        /* 查询:检查该项是否为基本件 以及是否存在配件*/

        /* 此处配件是指添加商品时附加的并且是设置了优惠价格的配件 此类配件都有parent_idgoods_number为1 */

        $sql = "SELECT b.goods_number,b.rec_id

                FROM ".$GLOBALS['ecs']->table('cart') . " a, ".$GLOBALS['ecs']->table('cart') . " b

                WHERE a.rec_id = '$key'

                AND a.session_id = '" .SESS_ID . "'

                AND a.extension_code <>'package_buy'

                AND b.parent_id = a.goods_id

                AND b.session_id = '" .SESS_ID . "'";

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

        //订货数量大于0

        if ($val > 0)

        {

            /* 判断是否为超出数量的优惠价格的配件 删除*/

            $row_num = 1;

            while ($offers_accessories_row =$GLOBALS['db']->fetchRow($offers_accessories_res))

            {

                if ($row_num > $val)

                {

                    $sql = "DELETE FROM" . $GLOBALS['ecs']->table('cart') .

                            " WHERE session_id = '" . SESS_ID . "' " .

                            "AND rec_id ='" . $offers_accessories_row['rec_id'] ."' LIMIT 1";

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

                }

                $row_num ++;

            }

            /* 处理超值礼包*/

            if ($goods['extension_code'] =='package_buy')

            {

                //更新购物车中的商品数量

                $sql = "UPDATE ".$GLOBALS['ecs']->table('cart').

                        " SET goods_number= '$val' WHERE rec_id='$key' AND session_id='" . SESS_ID . "'";

            }

            /* 处理普通商品或非优惠的配件*/

            else

            {

                $attr_id    = empty($goods['goods_attr_id']) ? array(): explode(',', $goods['goods_attr_id']);

                $goods_price =get_final_price($goods['goods_id'], $val, true, $attr_id);

                //更新购物车中的商品数量

                $sql = "UPDATE ".$GLOBALS['ecs']->table('cart').

                        " SET goods_number= '$val', goods_price = '$goods_price' WHERE rec_id='$key' AND session_id='" . SESS_ID . "'";

            }

        }

        //订货数量等于0

        else

        {

            /* 如果是基本件并且有优惠价格的配件则删除优惠价格的配件*/

            while ($offers_accessories_row =$GLOBALS['db']->fetchRow($offers_accessories_res))

            {

                $sql = "DELETE FROM ". $GLOBALS['ecs']->table('cart') .

                        " WHERE session_id= '" . SESS_ID . "' " .

                        "AND rec_id ='" . $offers_accessories_row['rec_id'] ."' LIMIT 1";

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

            }

            $sql = "DELETE FROM ".$GLOBALS['ecs']->table('cart').

                " WHERE rec_id='$key' AND session_id='" .SESS_ID. "'";

        }

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

        /* 删除所有赠品*/

        $sql = "DELETE FROM " .$GLOBALS['ecs']->table('cart') . " WHERE session_id = '" .SESS_ID."' AND is_gift <> 0";

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

        

        $result['rec_id'] = $key;

        $result['goods_number'] = $val;        

        $result['goods_subtotal'] = '';

        $result['total_desc'] = '';     

        $result['cart_info'] =insert_cart_info();  

        /* 计算合计*/

        $cart_goods = get_cart_goods();

        foreach ($cart_goods['goods_list'] as$goods )

        {

            if ($goods['rec_id'] == $key)

            {

                $result['goods_subtotal'] =$goods['subtotal'];

                break;

            }

        }

        $shopping_money =sprintf($_LANG['shopping_money'], $cart_goods['total']['goods_price']);

        $market_price_desc = sprintf($_LANG['than_market_price'],$cart_goods['total']['market_price'],$cart_goods['total']['saving'], $cart_goods['total']['save_rate']);

    

        /* 计算折扣*/

        $discount = compute_discount();

        $favour_name = empty($discount['name'])? '' : join(',', $discount['name']);

        $your_discount =sprintf($_LANG['your_discount'], $favour_name,price_format($discount['discount']));

        

        if ($discount['discount'] > 0)

        {

            $result['total_desc'] .=$your_discount . '<br />';

        }

        $result['total_desc'] .=$shopping_money;

        if ($_CFG['show_marketprice'])

        {

            $result['total_desc'] .= ',' .$market_price_desc;

        }

        die($json->encode($result));          

    }

    else

    {

        $result['error'] = 100;

        $result['message'] = '';

        die($json->encode($result));

    }                 

}

附两张箭头图片,上传至\themes\default\images\目录下即可

 

至此全部修改完成