- 積分
- 84
- 威望
- 84
- 金錢
- 3
|
1#
發表於 2010-5-4 00:05
| 只看該作者
帖子標題: [分享] 使用ajax更新購物車數量for 2.7
使用版本 2.7.0
修改前請先備份下列檔案:
themes/模板名稱/flow.dwt
flow.php
開啟 themes/模板名稱/flow.dwt
搜尋- name="goods_number[{$goods.rec_id}]"
複製代碼 會找到類似這段... (依據個人使用的模組不同會有些許差異)- <input class="TextInput" name="goods_number[{$goods.rec_id}]" id="goods_number_{$goods.rec_id}" value="{$goods.goods_number}" type="text" size="4" onkeydown="showdiv(this)"/>
複製代碼 置換成... (依據個人使用的模組不同會有些許差異)- <input class="TextInput" 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}" size="4" id="ECS_FORMBUY">
複製代碼 搜尋- <!-- {if $step eq "cart"} -->
複製代碼 在下面加入- <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_cart_ajax&rec_id=' + rec_id +'&number=' + number, changePriceResponse, 'GET', 'JSON');
- }
- /**
- * 接收返回的信息
- */
- function changePriceResponse(res)
- {
- document.getElementById('goods_number_' + res.rec_id).value = res.goods_number;
- if (res.error > 0)
- {
- alert(res.content);
- }
- else
- {
- document.getElementById('subtotal_'+res.rec_id).innerHTML = res.subtotal;//商品小計
- document.getElementById('cart_amount_desc').innerHTML = res.cart_amount_desc;//購物車商品總價說明
- document.getElementById('market_amount_desc').innerHTML = res.market_amount_desc;//購物車商品總市價說明
- }
- }
- </script>
複製代碼 搜尋替換為- <div id="subtotal_{$goods.rec_id}">{$goods.subtotal}</div>
複製代碼 搜尋替換為- <span id="cart_amount_desc">{$shopping_money}</span>
複製代碼 搜尋
{$market_price_desc}
替換為
<span id="market_amount_desc">{$market_price_desc}</span>
搜尋會找到類似這段... (依據個人使用的模組不同會有些許差異)- <input name="submit" type="submit" class="bnt_num6" value="{$lang.update_cart}" />
複製代碼 將上面整段移除
搜尋會找到類似這段... (依據個人使用的模組不同會有些許差異)- <input type="hidden" name="step" value="update_cart" />
複製代碼 將上面整段移除
存檔關閉後上傳...
開啟 flow.php
搜尋- elseif ($_REQUEST['step'] == 'update_cart')
複製代碼 在上面加入- elseif($_REQUEST['step'] == 'update_cart_ajax')
- {
- /*------------------------------------------------------ */
- //-- AJAX更新購物車
- /*------------------------------------------------------ */
- include_once('includes/cls_json.php');
- $json = new JSON();
- $result = array('error' => '', 'content' => '');
- $rec_id = $_GET['rec_id'];
- $number = $_GET['number'];
- #判斷輸入是否合法
- if(!is_numeric($number))
- {
- $result['error'] = '1';
- $result['content'] = $_LANG['goods_number_not_int'];
- die($json->encode($result));
- }
- $goods_number = array($_GET['rec_id'] => $_GET['number']);
- if (isset($goods_number) && is_array($goods_number))
- {
- if(flow_update_cart_ajax($goods_number)){
- die($json->encode(flow_update_cart_ajax($goods_number)));
- }
- }
- /* 取得商品列表,計算合計 */
- $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['goods_number'] = $number;
- $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']
- );
- $result['rec_id'] = $rec_id;
- die($json->encode($result));
- }
複製代碼 搜尋- function flow_update_cart($arr)
複製代碼 在上面加入- function flow_update_cart_ajax($arr)
- {
- foreach ($arr AS $key => $val)
- {
- $val = intval(make_semiangle($val));
- $sql = "SELECT `goods_id`, `goods_attr_id`, `extension_code`,`goods_number` 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);
- $result['error'] = '1';
- $result['rec_id'] = $key;
- /* 數量是否小於0 */
- if ($val <= 0)
- {
- $result['goods_number'] = $goods['goods_number'];
- $result['content'] = $GLOBALS['_LANG']['goods_number_not_int'];
- return $result;
- }
- /* 系統啟用了庫存,檢查輸入的商品數量是否有效 */
- if (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] != 'package_buy')
- {
- if ($row['goods_number'] < $val)
- {
- $result['goods_number'] = $goods['goods_number'];
- $result['content'] =sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'],$row['goods_number'], $row['goods_number']);
- return $result;
- }
- }
- elseif (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] == 'package_buy')
- {
- if (judge_package_stock($goods['goods_id'], $val))
- {
- $result['content'] =$GLOBALS['_LANG']['package_stock_insufficiency'];
- return $result;
- }
- }
- /* 檢查該項是否為基本件以及有沒有配件存在 */
- $sql = "SELECT a.goods_number, a.rec_id FROM " .$GLOBALS['ecs']->table('cart') . " AS b ".
- "LEFT JOIN " . $GLOBALS['ecs']->table('cart') . " AS a ".
- "ON a.parent_id = b.goods_id AND a.session_id = '" . SESS_ID . "' AND a.extension_code <> 'package_buy'".
- "WHERE b.rec_id = '$key'";
- $fittings = $GLOBALS['db']->getAll($sql);
- if ($val > 0)
- {
- foreach ($fittings AS $k => $v)
- {
- if ($v['goods_number'] != null && $v['rec_id'] != null)
- {
- /* 該商品有配件,更新配件的商品數量 */
- $num = ($v['goods_number']) > $val ? $val : $v['goods_number'];
- $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') .
- " SET goods_number = '$num' WHERE rec_id = $v[rec_id]";
- $GLOBALS['db']->query($sql);
- }
- }
- 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 . "'";
- }
- }
- else
- {
- if (is_object($fittings) && $fittings->goods_number != null && $fittings->rec_id != null)
- {
- $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart'). " WHERE rec_id=$fittings[rec_id]";
- $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);
- }
複製代碼 存檔關閉後上傳... |
-
1
評分次數
-
提醒您:
您在" ECSHOP 交流討論區"交流請遵守台灣法律規範," denise92"
發表的文章 《使用ajax更新購物車數量for 2.7》版權歸屬作者所有,如是轉貼版權歸屬原作者所有.本論壇不對其真實性做任何考證.
|