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購物車無刷新更新。
此文由 網站目錄_網站網址收錄與提交入口 編輯,未經允許不得轉載!: