用戶進入echsop購物流程的時候當需要更改購物車裡的商品數量的時候必須要手動點擊更新按鈕.
以下是通過AJAX方式來動態更新ECShop購物車頁面商品數量的解決辦法
一、前端頁面部分(flow.dwt)
在商品數量的input框添加對應的js函數
<INPUT onblur="changePrice(document.getElementById('goods_number_{$goods.rec_id}').value,{$goods.rec_id})" name="goods_number[{$goods.rec_id}]" id="goods_number_{$goods.rec_id}" value="{$goods.goods_number}" />
在文件最後添加一段js
<script type="text/javascript">
function changePrice(number,rec_id)
{
var attr = getSelectedAttributes(document.forms['ECS_FORMBUY']);
var qty = document.forms['ECS_FORMBUY'].elements['number'].value;
Ajax.call('flow.php', 'step=update_group_cart&rec_id=' + rec_id +'&number=' + number, changePriceResponse, 'GET', 'JSON');
}
function changePriceResponse(res)
{
if (res.error > 0)
{
document.getElementById('sysmsg_error').innerHTML = res.content;
document.all.sysmsg_error.style.display=";
}
else
{
if(document.all.sysmsg_error.style.display==")
{
document.all.sysmsg_error.style.display='none';
}
document.getElementById('subtotal_'+res.rec_id).innerHTML = res.subtotal;
document.getElementById('cart_amount').innerHTML = res.cart_amount;
}
}
</script>
二、在flow.php文件中插入對應的處理代碼:
elseif($_REQUEST['step'] == 'update_group_cart')
{
include_once('includes/cls_json.php');
$json = new JSON();
$result = array('error' => ", 'content' => ");
$rec_id = $_GET['rec_id'];
$number = $_GET['number'];
$group_buy = group_buy_info($_SESSION['extension_id'], $number);
if(!is_numeric($number))
{
$result['error'] = '1′;
$result['content'] ='請輸入合法數量';
die($json->encode($result));
}
if ($group_buy['restrict_amount'] > 0 && $number > ($group_buy['restrict_amount'] – $group_buy['valid_goods']))
{
$result['error'] = '1′;
$restrict_amount = $group_buy['restrict_amount'] – $group_buy['valid_goods'];
$result['content'] ='您最多可買'.$restrict_amount.'件';
die($json->encode($result));
}
$sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_number = '$number' WHERE rec_id = $rec_id";
$GLOBALS['db']->query($sql);
$subtotal = $GLOBALS['db']->getONE("select goods_price * goods_number AS subtotal from ".$GLOBALS['ecs']->table('cart')." where rec_id = $rec_id");
$cart_amount = cart_amount(",$_SESSION['flow_type']);
$result['subtotal'] = price_format($subtotal, false);
$result['cart_amount'] = price_format($cart_amount, false);
$result['rec_id'] = $rec_id;
die($json->encode($result));
}
記住要先清空echsop的Cache頁面。
2 則留言:
您好,網路上找到您的教程覺得你很用心,也剛好找到需要的功能,布過這個功能我拭布泰出來,在一.input裡有2個id可以嗎?(id="goods_number_{$goods.rec_id}" &id="ECS_FORMBUY")!?
還有在二.是把整段拷貝到flow.php嗎?
可以再說明詳細一點嗎?感激不盡~謝謝
1.input 部份有點錯誤~ 有更正了.
2.對~ 你可以 copy 到 elseif ($_REQUEST['step'] == 'link_buy') 前面.
張貼留言