ecshop購物車頁面使用ajax更新商品數量、小計、總價,點選加號和減號
阿新 • • 發佈:2019-02-01
第一步
我先把 我的購物車頁面flow.dwt的商品列表部分的程式碼貼出來,這樣比較起來方便一點
<div class="flowBox"> <h6><span>{$lang.goods_list}</span></h6> <div class="flowBox_in"> <form id="formCart" name="formCart" method="post" action="flow.php"> <table width="99%" align="center" border="0" cellpadding="5" cellspacing="1" bgcolor="#aacded"> <tr> <th bgcolor="#ebf4fb">{$lang.goods_name}</th> <!-- {if $show_marketprice} 顯示市場價 --> <th bgcolor="#ebf4fb">{$lang.market_prices}</th> <!-- {/if} --> <th bgcolor="#ebf4fb">{$lang.shop_prices}</th> <th bgcolor="#ebf4fb">{$lang.number}</th> <th bgcolor="#ebf4fb">{$lang.subtotal}</th> <th bgcolor="#ebf4fb">{$lang.handle}</th> </tr> <!-- {foreach from=$goods_list item=goods} --> <tr id="tr_goods_{$goods.rec_id}"> <td bgcolor="#ffffff" align="center"> <!-- {if $goods.goods_id gt 0 && $goods.extension_code neq 'package_buy'} 商品 --> <!-- {if $show_goods_thumb eq 1} --> <a href="goods.php?id={$goods.goods_id}" target="_blank" class="f6">{$goods.goods_name}</a> <!-- {elseif $show_goods_thumb eq 2} --> <a href="goods.php?id={$goods.goods_id}" target="_blank"><img style="width:50px" src="{$goods.goods_thumb}" border="0" title="{$goods.goods_name|escape:html}" /></a> <!-- {else} --> <a href="goods.php?id={$goods.goods_id}" target="_blank"><img style="width:50px" src="{$goods.goods_thumb}" border="0" title="{$goods.goods_name|escape:html}" /></a><br /> <a href="goods.php?id={$goods.goods_id}" target="_blank" class="f6">{$goods.goods_name}</a> <!-- {/if} --> <!-- {if $goods.parent_id gt 0} 配件 --> <span style="color:#FF0000">({$lang.accessories})</span> <!-- {/if} --> <!-- {if $goods.is_gift gt 0} 贈品 --> <span style="color:#FF0000">({$lang.largess})</span> <!-- {/if} --> <!-- {elseif $goods.goods_id gt 0 && $goods.extension_code eq 'package_buy'} --> <a href="javascript:void(0)" onclick="setSuitShow({$goods.goods_id})" class="f6">{$goods.goods_name}<span style="color:#FF0000;">({$lang.remark_package})</span></a> <div id="suit_{$goods.goods_id}" style="display:none"> <!-- {foreach from=$goods.package_goods_list item=package_goods_list} --> <a href="goods.php?id={$package_goods_list.goods_id}" target="_blank" class="f6">{$package_goods_list.goods_name}</a><br /> <!-- {/foreach} --> </div> <!-- {else} 優惠活動 --> {$goods.goods_name} <!-- {/if} --> </td> <!-- {if $show_marketprice} 顯示市場價 --> <td align="center" bgcolor="#ffffff">{$goods.market_price}</td> <!-- {/if} --> <td align="center" bgcolor="#ffffff">{$goods.goods_price}</td> <td align="center" bgcolor="#ffffff"> <!-- {if $goods.goods_id gt 0 && $goods.is_gift eq 0 && $goods.parent_id eq 0} 普通商品可修改數量 --> <a onclick="changenum({$goods.rec_id},-1)" style="height:12px; color:#000; line-height:12px; padding:0px 4px; background:#fff; border:1px #eee solid; display:inline-block;" href="javascript:void(0)" >-</a> <input type="text" name="goods_number[{$goods.rec_id}]" id="goods_number_{$goods.rec_id}" value="{$goods.goods_number}" size="3"class="inputBg" style="text-align:center " onchange="change_goods_number({$goods.rec_id},this.value)"/> <a onclick="changenum({$goods.rec_id},1)" style="height:12px; color:#000; line-height:12px; padding:0px 4px; background:#fff; border:1px #eee solid; display:inline-block;" href="javascript:void(0)" >+</a> <!-- {else} --> {$goods.goods_number} <!-- {/if} --> </td> <td align="center" bgcolor="#ffffff" id="goods_subtotal_{$goods.rec_id}"> <span>{$goods.subtotal}</span> </td> <td align="center" bgcolor="#ffffff"> <a href="javascript:if (confirm('{$lang.drop_goods_confirm}')) location.href='flow.php?step=drop_goods&id={$goods.rec_id}'; " class="f6">{$lang.drop}</a> <!-- {if $smarty.session.user_id gt 0 && $goods.extension_code neq 'package_buy'} 如果登入了,可以加入收藏 --> <a href="javascript:if (confirm('{$lang.drop_goods_confirm}')) location.href='flow.php?step=drop_to_collect&id={$goods.rec_id}'; " class="f6">{$lang.drop_to_collect}</a> <!-- {/if} --> </td> </tr> <!-- {/foreach} --> </table> <table width="99%" align="center" border="0" cellpadding="5" cellspacing="1" bgcolor="#aacded"> <tr> <td bgcolor="#ebf4fb" id="total_desc"> <!-- {if $discount gt 0} -->{$your_discount}<br /><!-- {/if} --> {$shopping_money}<!-- {if $show_marketprice} -->,{$market_price_desc}<!-- {/if} --> </td> <td align="right" bgcolor="#ebf4fb"> <input type="button" value="{$lang.clear_cart}" class="bnt_blue_1" onclick="location.href='flow.php?step=clear'" /> </td> </tr> </table>
第二步
我把js函式寫出來
<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) { if(goods_number >= 1){ 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; var tr_goods = 'tr_goods_' + 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).style.display = 'none'; $$(tr_goods).innerHTML = ''; } $$('total_desc').innerHTML =result.total_desc;//更新合計 if ($$('ECS_CARTINFO')) {//更新購物車數量 $$('ECS_CARTINFO').innerHTML = result.cart_info; } } else if (result.message != '') { alert(result.message); } } </script>
以上兩部分是flow.dwt頁面全部的程式碼
第三步
也就是要處理ajax請求的php程式碼,是flow.php檔案
//-- 更新購物車商品數量 /*------------------------------------------------------ */ 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)); } //查詢: $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_idgoods_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']); $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; if ($_CFG['show_marketprice']) { $result['total_desc'] .= ',' .$market_price_desc; } die($json->encode($result)); } else { $result['error'] = 100; $result['message'] = ''; die($json->encode($result)); } }