水微晶 埋線 prp
返回列表 回復 發帖

帖子標題:[分享] 使用ajax更新購物車數量for 2.7

使用版本 2.7.0

修改前請先備份下列檔案:
    themes/模板名稱/flow.dwt
    flow.php

開啟 themes/模板名稱/flow.dwt

搜尋
  1. name="goods_number[{$goods.rec_id}]"
複製代碼
會找到類似這段... (依據個人使用的模組不同會有些許差異)
  1. <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)"/>
複製代碼
置換成... (依據個人使用的模組不同會有些許差異)
  1. <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">
複製代碼
搜尋
  1. <!-- {if $step eq "cart"} -->
複製代碼
在下面加入
  1. <script type="text/javascript">
  2. /**
  3. * 點選可選屬性或改變數量時修改商品價格的函數
  4. */
  5. function changePrice(number,rec_id)
  6. {
  7. //var attr = getSelectedAttributes(document.forms['ECS_FORMBUY']);
  8. // var qty = document.forms['ECS_FORMBUY'].elements['number'].value;
  9. Ajax.call('flow.php', 'step=update_cart_ajax&rec_id=' + rec_id +'&number=' + number, changePriceResponse, 'GET', 'JSON');
  10. }
  11. /**
  12. * 接收返回的信息
  13. */
  14. function changePriceResponse(res)
  15. {
  16. document.getElementById('goods_number_' + res.rec_id).value = res.goods_number;
  17. if (res.error > 0)
  18. {
  19. alert(res.content);
  20. }
  21. else
  22. {
  23. document.getElementById('subtotal_'+res.rec_id).innerHTML = res.subtotal;//商品小計
  24. document.getElementById('cart_amount_desc').innerHTML = res.cart_amount_desc;//購物車商品總價說明
  25. document.getElementById('market_amount_desc').innerHTML = res.market_amount_desc;//購物車商品總市價說明
  26. }
  27. }
  28. </script>
複製代碼
搜尋
  1. {$goods.subtotal}
複製代碼
替換為
  1. <div id="subtotal_{$goods.rec_id}">{$goods.subtotal}</div>
複製代碼
搜尋
  1. {$shopping_money}
複製代碼
替換為
  1. <span id="cart_amount_desc">{$shopping_money}</span>
複製代碼
搜尋
{$market_price_desc}
替換為
<span id="market_amount_desc">{$market_price_desc}</span>

搜尋
  1. {$lang.update_cart}
複製代碼
會找到類似這段... (依據個人使用的模組不同會有些許差異)
  1. <input name="submit" type="submit" class="bnt_num6" value="{$lang.update_cart}" />
複製代碼
將上面整段移除

搜尋
  1. value="update_cart"
複製代碼
會找到類似這段... (依據個人使用的模組不同會有些許差異)
  1. <input type="hidden" name="step" value="update_cart" />
複製代碼
將上面整段移除

存檔關閉後上傳...

開啟 flow.php

搜尋
  1. elseif ($_REQUEST['step'] == 'update_cart')
複製代碼
在上面加入
  1. elseif($_REQUEST['step'] == 'update_cart_ajax')
  2. {
  3. /*------------------------------------------------------ */
  4. //-- AJAX更新購物車
  5. /*------------------------------------------------------ */
  6. include_once('includes/cls_json.php');
  7. $json = new JSON();
  8. $result = array('error' => '', 'content' => '');
  9. $rec_id = $_GET['rec_id'];
  10. $number = $_GET['number'];
  11. #判斷輸入是否合法
  12. if(!is_numeric($number))
  13. {
  14. $result['error'] = '1';
  15. $result['content'] = $_LANG['goods_number_not_int'];
  16. die($json->encode($result));
  17. }
  18. $goods_number = array($_GET['rec_id'] => $_GET['number']);
  19. if (isset($goods_number) && is_array($goods_number))
  20. {
  21. if(flow_update_cart_ajax($goods_number)){
  22. die($json->encode(flow_update_cart_ajax($goods_number)));
  23. }
  24. }
  25. /* 取得商品列表,計算合計 */
  26. $cart_goods = get_cart_goods();
  27. #計算此訂單總價
  28. $subtotal = $GLOBALS['db']->getONE("select goods_price * goods_number AS subtotal from ".$GLOBALS['ecs']->table('cart')." where rec_id = $rec_id");
  29. #購物車商品總金額
  30. $result['goods_number'] = $number;
  31. $result['subtotal'] = price_format($subtotal, false);
  32. $result['cart_amount_desc'] = sprintf($_LANG['shopping_money'], $cart_goods['total']['goods_price']);
  33. $result['market_amount_desc'] = sprintf($_LANG['than_market_price'], $cart_goods['total']['market_price'], $cart_goods['total']['saving'], $cart_goods['total']['save_rate']
  34. );
  35. $result['rec_id'] = $rec_id;
  36. die($json->encode($result));
  37. }
複製代碼
搜尋
  1. function flow_update_cart($arr)
複製代碼
在上面加入
  1. function flow_update_cart_ajax($arr)
  2. {
  3. foreach ($arr AS $key => $val)
  4. {
  5. $val = intval(make_semiangle($val));
  6. $sql = "SELECT `goods_id`, `goods_attr_id`, `extension_code`,`goods_number` FROM" .$GLOBALS['ecs']->table('cart').
  7. " WHERE rec_id='$key' AND session_id='" . SESS_ID . "'";
  8. $goods = $GLOBALS['db']->getRow($sql);
  9. $sql = "SELECT g.goods_name, g.goods_number ".
  10. "FROM " .$GLOBALS['ecs']->table('goods'). " AS g, ".
  11. $GLOBALS['ecs']->table('cart'). " AS c ".
  12. "WHERE g.goods_id = c.goods_id AND c.rec_id = '$key'";
  13. $row = $GLOBALS['db']->getRow($sql);
  14. $result['error'] = '1';
  15. $result['rec_id'] = $key;
  16. /* 數量是否小於0 */
  17. if ($val <= 0)
  18. {
  19. $result['goods_number'] = $goods['goods_number'];
  20. $result['content'] = $GLOBALS['_LANG']['goods_number_not_int'];
  21. return $result;
  22. }
  23. /* 系統啟用了庫存,檢查輸入的商品數量是否有效 */
  24. if (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] != 'package_buy')
  25. {
  26. if ($row['goods_number'] < $val)
  27. {
  28. $result['goods_number'] = $goods['goods_number'];
  29. $result['content'] =sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'],$row['goods_number'], $row['goods_number']);
  30. return $result;
  31. }
  32. }
  33. elseif (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] == 'package_buy')
  34. {
  35. if (judge_package_stock($goods['goods_id'], $val))
  36. {
  37. $result['content'] =$GLOBALS['_LANG']['package_stock_insufficiency'];
  38. return $result;
  39. }
  40. }
  41. /* 檢查該項是否為基本件以及有沒有配件存在 */
  42. $sql = "SELECT a.goods_number, a.rec_id FROM " .$GLOBALS['ecs']->table('cart') . " AS b ".
  43. "LEFT JOIN " . $GLOBALS['ecs']->table('cart') . " AS a ".
  44. "ON a.parent_id = b.goods_id AND a.session_id = '" . SESS_ID . "' AND a.extension_code <> 'package_buy'".
  45. "WHERE b.rec_id = '$key'";
  46. $fittings = $GLOBALS['db']->getAll($sql);
  47. if ($val > 0)
  48. {
  49. foreach ($fittings AS $k => $v)
  50. {
  51. if ($v['goods_number'] != null && $v['rec_id'] != null)
  52. {
  53. /* 該商品有配件,更新配件的商品數量 */
  54. $num = ($v['goods_number']) > $val ? $val : $v['goods_number'];

  55. $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') .
  56. " SET goods_number = '$num' WHERE rec_id = $v[rec_id]";
  57. $GLOBALS['db']->query($sql);
  58. }
  59. }

  60. if ($goods['extension_code'] == 'package_buy')
  61. {
  62. /* 更新購物車中的商品數量 */
  63. $sql = "UPDATE " .$GLOBALS['ecs']->table('cart').
  64. " SET goods_number = '$val' WHERE rec_id='$key' AND session_id='" . SESS_ID . "'";
  65. }
  66. else
  67. {
  68. $attr_id = empty($goods['goods_attr_id']) ? array() : explode(',', $goods['goods_attr_id']);
  69. $goods_price = get_final_price($goods['goods_id'], $val, true, $attr_id);

  70. /* 更新購物車中的商品數量 */
  71. $sql = "UPDATE " .$GLOBALS['ecs']->table('cart').
  72. " SET goods_number = '$val', goods_price = '$goods_price' WHERE rec_id='$key' AND session_id='" . SESS_ID . "'";
  73. }
  74. }
  75. else
  76. {
  77. if (is_object($fittings) && $fittings->goods_number != null && $fittings->rec_id != null)
  78. {
  79. $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart'). " WHERE rec_id=$fittings[rec_id]";
  80. $GLOBALS['db']->query($sql);
  81. }

  82. $sql = "DELETE FROM " .$GLOBALS['ecs']->table('cart').
  83. " WHERE rec_id='$key' AND session_id='" .SESS_ID. "'";
  84. }

  85. $GLOBALS['db']->query($sql);
  86. }

  87. /* 刪除所有贈品 */
  88. $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '" .SESS_ID. "' AND is_gift <> 0";
  89. $GLOBALS['db']->query($sql);
  90. }
複製代碼
存檔關閉後上傳...
1

評分次數

提醒您: 您在"ECSHOP 交流討論區"交流請遵守台灣法律規範,"denise92" 發表的文章《使用ajax更新購物車數量for 2.7》版權歸屬作者所有,如是轉貼版權歸屬原作者所有.本論壇不對其真實性做任何考證.
感謝您的分享~剛已實際操作了兩遍~但出現價格無法同步更新的狀況~
不知有可能我的哪個步驟出錯呢?謝謝~指點迷津
提醒您: 您在"ECSHOP 交流討論區"交流請遵守台灣法律規範,"life10252001" 發表的文章《》版權歸屬作者所有,如是轉貼版權歸屬原作者所有.本論壇不對其真實性做任何考證.
板大感謝您的方法~已經成功實現以ajax更新購物車數量功能。
提醒您: 您在"ECSHOP 交流討論區"交流請遵守台灣法律規範,"life10252001" 發表的文章《》版權歸屬作者所有,如是轉貼版權歸屬原作者所有.本論壇不對其真實性做任何考證.
更新一下程式碼喔~~~ 原帖不給編輯了XD

請將flow.php中的這段通通刪除掉~ ^^
  1. #判斷輸入是否合法
  2. if(!is_numeric($number))
  3. {
  4. $result['error'] = '1';
  5. $result['content'] = $_LANG['goods_number_not_int'];
  6. die($json->encode($result));
  7. }
複製代碼
提醒您: 您在"ECSHOP 交流討論區"交流請遵守台灣法律規範,"denise92" 發表的文章《》版權歸屬作者所有,如是轉貼版權歸屬原作者所有.本論壇不對其真實性做任何考證.
不錯用的教學,要來好好的了解一下.
提醒您: 您在"ECSHOP 交流討論區"交流請遵守台灣法律規範,"f117a" 發表的文章《》版權歸屬作者所有,如是轉貼版權歸屬原作者所有.本論壇不對其真實性做任何考證.
返回列表