ECSHOP无刷新更新修改购物车数量

ECSHOP无刷新更新修改购物车数量
需要修改的文件
/themes/default/flow.dwt
/flow.php
一、打开/themes/default/flow.dwt文件
找到如下代码:

<!-- {foreach from=$goods_list item=goods} -->

共有两处,这里改第一处(53行左右),将下一行的修改成:

<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)"/>

修改成:

<div class="cartnum">
<a style="cursor:pointer" onclick="changenum({$goods.rec_id},-1)" class="imgl"></a>
<input type="text" name="goods_number[{$goods.rec_id}]" id="goods_number_{$goods.rec_id}" value="{$goods.goods_number}" size="4" onchange="change_goods_number({$goods.rec_id},this.value)" class="inum">
<a onclick="changenum({$goods.rec_id},1)" href="javascript:void(0)" class="imgr"></a>
</div>

继续找到如下代码:

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

修改成:

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

继续找到如下代码:

<!-- {if $discount gt 0} -->{$your_discount}<br /><!-- {/if} -->

共两处,这里改第一处,将上一行,修改成:

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

继续找到如下代码:

<!-- {if $smarty.session.user_id gt 0} -->

在该行前面添加如下代码:

 <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>

二、打开/themes/default/style.css文件
在最底部加入以下代码:

/* 开始位置_新增加 by www.edait.cn */
.cartnum .inum{float:left;margin:0 5px;width:35px;height:1pc;border:0;border:1px solid #aeaeae;text-align:center;line-height:1pc}
.cartnum .imgl{background:url(images/gdown.gif) no-repeat}
.cartnum .imgl,.cartnum .imgr{float:left;display:block;margin-top:4px;width:14px;height:13px}
.cartnum .imgr{background:url(images/gup.gif) no-repeat}
/* 结束位置_新增加 by www.edait.cn */

三、打开/flow.php文件
找到如下代码:

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

在以上这段代码的上面加:

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));
        }
        if (!is_numeric($_POST['goods_number']))
        {
            $result['error'] = '1';
            $result['message'] =$_LANG['goods_number_not_int'];
            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_id goods_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']);
        if ($_CFG['show_marketprice'])
        {
            $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;
        $result['total_desc'] .= $market_price_desc;
        die($json->encode($result));
    }
    else
    {
        $result['error'] = 100;
        $result['message'] = '';
        die($json->encode($result));
    }
}

四、打开/user.php文件
将以下插件文件夹里的文件对应你的模板上传
/themes/default/images/gdown.gif
/themes/default/images/gup.gif

通过以上操作便可实现ECSHOP无刷新更新修改购物车数量功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅夏渐至

你得鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值