修改文件:
1. flow.dwt 模板,
2. flow.php
首先
第一步 :
修改购物车模板:
添加 数量减少/增加 按钮(这里用的span),input文本框按照下边参数设置好。
<div class="zz fl">
<span onclick="red_num({$goods.rec_id},{$goods.goods_id});">-</span>
<input type="text" name="goods_number[{$goods.rec_id}]" class="input2" value="{$goods.goods_number}" id="goods_number_{$goods.rec_id}" onchange="change_price({$goods.rec_id},{$goods.goods_id})" />
<span onclick="add_num({$goods.rec_id},{$goods.goods_id})">+</span>
</div>
第二步:
模板文件添加JS
<script type="text/javascript">
function add_num(rec_id,goods_id)
{
document.getElementById("goods_number_"+rec_id+"").value++;
var number = document.getElementById("goods_number_"+rec_id+"").value++;
Ajax.call('flow.php', 'step=update_group_cart&rec_id=' + rec_id+'&number=' + number+'&goods_id=' + goods_id, changePriceRespone, 'GET', 'JSON');
}
function red_num(rec_id,goods_id)
{
if (document.getElementById("goods_number_"+rec_id+"").value>1)
{
document.getElementById("goods_number_"+rec_id+"").value--;
}
var number = document.getElementById("goods_number_"+rec_id+"").value;
Ajax.call('flow.php', 'step=update_group_cart&rec_id=' + rec_id+'&number=' + number+'&goods_id=' + goods_id, changePriceRespone, 'GET', 'JSON');
}
function change_price(rec_id,goods_id)
{
var number = document.getElementById("goods_number_"+rec_id+"").value;
Ajax.call('flow.php','step=update_group_cart&rec_id=' + rec_id+'&number=' + number+'&goods_id=' + goods_id, changePriceRespone, 'GET', 'JSON');
}
function changePriceRespone(result)
{
if(result.error == 1)
{
alert(result.content);
document.getElementById("goods_number_"+result.rec_id+"").value = result.number;
}else{
document.getElementById('subtotal_'+result.rec_id).innerHTML = result.subtotal;//商品总价
document.getElementById('cart_amount_desc').innerHTML = result.cart_amount_desc;//购物车商品总价说明
document.getElementById('market_amount_desc').innerHTML = result.market_amount_desc;//购物车商品总市价说明
}
}
</script>
顺便模板文件数量、价格计算添加ID
<span id="subtotal_{$goods.rec_id}">{$goods.subtotal}</span>
<span id="cart_amount_desc">{$shopping_money}</span>
<span id="market_amount_desc">{$market_price_desc}</span>
第三步:
修改php文件
搜索:$_REQUEST['step'] == 'consignee' 在上方添加
elseif($_REQUEST['step']=='update_group_cart')
{
//2016-05-24 cart_update
include_once('includes/cls_json.php');
$result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => '');
$json = new JSON;
$rec_id = $_GET['rec_id'];
$number = $_GET['number'];
$goods_id = $_GET['goods_id'];
$result['rec_id'] = $rec_id;
if ($GLOBALS['_CFG']['use_storage'] == 1)
{
$goods_number = $GLOBALS['db']->getOne("select goods_number from ".$GLOBALS['ecs']->table('goods')." where goods_id='$goods_id'");
if($number>$goods_number)
{
$result['error'] = '1';
$result['content'] ='对不起,您选择的数量超出库存您最多可购买'.$goods_number."件";
$result['number']=$goods_number;
die($json->encode($result));
}
}
$sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_number = '$number' WHERE rec_id = $rec_id";
$GLOBALS['db']->query($sql);
/* 取得商品列表,计算合计 */
$cart_goods = get_cart_goods();
$subtotal = $GLOBALS['db']->getONE("select goods_price * goods_number AS subtotal from ".$GLOBALS['ecs']->table('cart')." where rec_id = $rec_id");
$result['subtotal'] = price_format($subtotal, false);
$result['cart_amount_desc'] = sprintf($_LANG['shopping_money'],$cart_goods['total']['goods_price']);
$result['market_amount_desc'] = sprintf($_LANG['than_market_price'],$cart_goods['total']['market_price'],$cart_goods['total']['saving'],$cart_goods['total']['save_rate']);
die($json->encode($result));
}
到这里完成Ajax异步数量增减!!!