goods_cart.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * 删除购物车
  3. * @param cart_id
  4. */
  5. function drop_cart_item(cart_id){
  6. var parent_tr = $('#cart_item_' + cart_id).parent();
  7. var url = HOMESITEURL+'/Cart/del.html';
  8. layer.confirm('确认删除吗?', {
  9. btn: ['确定', '取消'],
  10. title: false,
  11. }, function (cart_confirm) {
  12. $.get(url, {'cart_id': cart_id}, function (result) {
  13. if (result.state) {
  14. //删除成功
  15. if (result.quantity == 0) {//判断购物车是否为空
  16. window.location.reload(); //刷新
  17. } else {
  18. $('tr[ds_group="' + cart_id + '"]').remove();//移除本商品或本套装
  19. if (parent_tr.children('tr').length == 2) {//只剩下店铺名头和店铺合计尾,则全部移除
  20. parent_tr.remove();
  21. }
  22. calc_cart_price();
  23. }
  24. } else {
  25. alert(result.msg);
  26. }
  27. }, 'json');
  28. layer.close(cart_confirm);
  29. });
  30. }
  31. /**
  32. * 更改购物车数量
  33. * @param cart_id
  34. * @param input
  35. */
  36. function change_quantity(cart_id, input){
  37. var subtotal = $('#item' + cart_id + '_subtotal');
  38. //暂存为局部变量,否则如果用户输入过快有可能造成前后值不一致的问题
  39. var _value = input.value;
  40. var url = HOMESITEURL+'/Cart/update.html';
  41. $.get(url,{'cart_id': cart_id,'quantity': _value}, function(result){
  42. $(input).attr('changed', _value);
  43. if(result.state == 'true'){
  44. $('#item' + cart_id + '_price').html(number_format(result.goods_price,2));
  45. subtotal.html(number_format(result.subtotal,2));
  46. $('#cart_id'+cart_id).val(cart_id+'|'+_value);
  47. }
  48. if(result.state == 'invalid'){
  49. subtotal.html(0.00);
  50. $('#cart_id'+cart_id).remove();
  51. $('tr[ds_group="'+cart_id+'"]').addClass('item_disabled');
  52. $(input).parent().next().html('');
  53. $(input).parent().removeClass('ws0').html('已下架');
  54. layer.msg(result.msg);
  55. return;
  56. }
  57. if(result.state == 'shortage'){
  58. $('#item' + cart_id + '_price').html(number_format(result.goods_price,2));
  59. $('#cart_id'+cart_id).val(cart_id+'|'+result.goods_num);
  60. $(input).val(result.goods_num);
  61. layer.msg(result.msg);
  62. return;
  63. }
  64. if(result.state == '') {
  65. //更新失败
  66. layer.msg(result.msg);
  67. $(input).val($(input).attr('changed'));
  68. }
  69. calc_cart_price();
  70. }, 'json');
  71. }
  72. /**
  73. * 购物车减少商品数量
  74. * @param cart_id
  75. */
  76. function decrease_quantity(cart_id){
  77. var item = $('#input_item_' + cart_id);
  78. var orig = Number(item.val());
  79. if(orig > 1){
  80. item.val(orig - 1);
  81. item.keyup();
  82. }
  83. }
  84. /**
  85. * 购物车增加商品数量
  86. * @param cart_id
  87. */
  88. function add_quantity(cart_id){
  89. var item = $('#input_item_' + cart_id);
  90. var orig = Number(item.val());
  91. item.val(orig + 1);
  92. item.keyup();
  93. }
  94. /**
  95. * 购物车商品统计
  96. */
  97. function calc_cart_price() {
  98. //每个店铺商品价格小计
  99. obj = $('table[ds_type="table_cart"]');
  100. if(obj.children('tbody').length==0) return;
  101. //购物车已选择商品的总价格
  102. var allTotal = 0;
  103. obj.children('tbody').each(function(){
  104. //购物车每个店铺已选择商品的总价格
  105. var eachTotal = 0;
  106. $(this).find('em[ds_type="eachGoodsTotal"]').each(function(){
  107. if ($(this).parent().parent().find('input[type="checkbox"]').eq(0).prop('checked') != true)
  108. {
  109. return;
  110. }
  111. eachTotal = eachTotal + parseFloat($(this).html());
  112. });
  113. allTotal += eachTotal;
  114. $(this).children('tr').last().find('em[ds_type="eachStoreTotal"]').eq(0).html(number_format(eachTotal,2));
  115. });
  116. $('#cartTotal').html(number_format(allTotal,2));
  117. }
  118. $(function(){
  119. calc_cart_price();
  120. $('#selectAll').on('click',function(){
  121. if ($(this).prop('checked')) {
  122. $('input[type="checkbox"]').prop('checked',true);
  123. $('input[type="checkbox"]:disabled').prop('checked',false);
  124. } else {
  125. $('input[type="checkbox"]').prop('checked',false);
  126. }
  127. calc_cart_price();
  128. });
  129. $('input[ds_type="eachGoodsCheckBox"]').on('click',function(){
  130. if (!$(this).prop('checked')) {
  131. $('#selectAll').prop('checked',false);
  132. }
  133. calc_cart_price();
  134. });
  135. $('#next_submit').on('click',function(){
  136. if ($(document).find('input[ds_type="eachGoodsCheckBox"]:checked').size() == 0) {
  137. layer.msg('请选中要结算的商品');
  138. return false;
  139. }else {
  140. $('#form_buy').submit();
  141. }
  142. });
  143. });