ecshop的购物车使用是相当的不方便.ecshop购物车一旦加入了商品,就必须点更新数量的按扭才能够更新.这样对ecshop使用者相当的不方便。我们将结合ecshop ajax的思路。来讲讲用ecshop的ajax无刷新更新购物车.
1:首先要包含js/shopping_flow.js 该文件主要是针对ecshop购买流程控制的js.我们将增加以下函数.
function submit_update_cart(rec_id){
var goods_number = document.getElementById("goods_number_"+rec_id).value;
Ajax.call('flow.php?step=ajax_update_cart', 'goods_number=' + goods_number+'&rec_id='+rec_id, submit_update_cartResponse_cart, 'GET', 'JSON');
}
2:在flow.dwt中。我们要修改input输入框.<input type="text" name="goods_number[{$goods.rec_id}]" id="goods_number_{$goods.rec_id}"
3:在ecshop的购物车函数中。什么json来结合ecshop ajax处理更新结果.部分程序如下
include_once('includes/cls_json.php');
$result = array('error' => '', 'content' => '', 'fanliy_number' => '0', 'rec_id' => '');
$json = new JSON();
/* AJAX修改购物车 */
$rec_id = $_REQUEST['rec_id']; //购物车ID
$goods_number = $_REQUEST['goods_number'];//
/* 判断库存 */
$num = $db -> getOne("select g.goods_number from ".$ecs->table('goods')." g ,".$ecs->table('cart')." c where c.rec_id = '$rec_id' and g.goods_id = c.goods_id ");
if($goods_number > $num){
$goods_number = $num;
$result['error'] = 1;
$result['fanliy_number']= $num;
$result['rec_id'] = $rec_id;
$result['content'] = '该商品库存不足'.$goods_number." 件,只有".$num."件";
die($json->encode($result));
}
/* 修改商品购物车 */
$sql = "update ".$ecs->table('cart')." set goods_number = '".$goods_number."' where rec_id = '".$rec_id."' and session_id = '" . SESS_ID . "' ";
$db -> query($sql);
4:通过flow.php中的php.返回更新数量后的结果.
function submit_update_cartResponse_cart(result){
if(result.error == '1'){
document.getElementById("goods_number_"+result.rec_id).value = result.fanliy_number;
alert(result.content)
}else{
var layer = document.getElementById("xianshi_price");
layer.innerHTML = (typeof result == "object") ? result.content : result;
}
}
通过以上ecshop二次开发例子,我们最模板完成了ecshop购物车无刷新更新。
(责任编辑:最模板) |