基于ecshop商城开发优惠券(红包)叠加使用功能

一般商城的优惠券(红包)都是只能使用一张的,但想要叠加使用的话,首先要新增字段来区别设置可不可叠加类型

这里写图片描述

input type=”hidden” name=”bonus” id=”bonus_id_{$k}” value=”0”

设置input隐藏域,id跟数组键值关联,选中的话value为红包的id,默认为0。
再把选中的红包ID都以字符串形式存到name为bonus1的input隐藏域value中,如:12,13,15… 用逗号隔开。

  <div class="bonus_part"> <p><span>优惠券</span> </p> <ul class="bonus_list_business">   <input type="hidden" name="bonus1" id="bonus_id" value="">  <input type="hidden" name="bonus" id="bonus_id_{$k}" value="0"> <input type="hidden" name="bonus_overlap" id="overlap_{$k}" {if $bonus.overlap eq 1 } value="1" {else} value="0" {/if}> <li {if $order.bonus_id eq $bonus.bonus_id} {/if} onclick="selectBonus2(this, {$bonus.bonus_id},{$k})">  <div class="bonus_info_business"> <span><em>¥{$bonus.bonus_money_formated}</em><strong>满{$bonus.min_goods_amount}可用</strong></span> <p><b>{$bonus.type_name}</b><label>(优惠券) ({if $bonus.overlap eq 1 } 可叠加 {else} 不可叠加 {/if})</label></p> </div> <div class="bonus_date_business"> <p>截止{$bonus.use_end_date}</p> </div> <u></u><font></font> </li>  </ul> <b id="no_overlap" style="color: red;margin-left: 35%;display: none;">你选择的优惠券不可叠加,不能再选择其他优惠券!</b> </div>  <div style="text-align: center;margin:8px auto;"> <input style="margin-top:25px;" type="image" src="images/bnt_subOrder.png" /> <input type="hidden" name="step" value="done" /> </div>


我的思路是这样的:

当选择的优惠券不可叠加时,value等于对应的红包id,其他优惠券设置为不可点击事件,并把其他优惠券的value都变为0(防止之前点击了可叠加的券后再点击不可叠加券);
取消选中时则恢复点击事件,其value等于0。

这里写图片描述


当选择的优惠券可叠加时,value等于对应的红包id, 取消选中时其value等于0。

这里写图片描述

/ ** * 可判断红包是否可叠加,并可同时选中/取消多个叠加红包 * @param obj 选中的li * @param value 红包的id * @param k 选中红包对应的键值 */ function selectBonus2(obj,value,k) { $(obj).toggleClass('checkbox_frame'); var is_overlap = document.getElementById("overlap_" + k).value; var num = document.getElementsByName("bonus");  if (is_overlap == 0) {  if ($(obj).hasClass("checkbox_frame")) { $(obj).css('border', '1px solid red'); $(obj).siblings().removeClass("checkbox_frame"); $(obj).siblings().css('border', '1px solid #ddd'); $(obj).siblings().css('pointer-events', 'none');  document.getElementById("no_overlap").style.display = "block";  for (var i = 0; i < num.length; i++) { if (k == i) { document.getElementById("bonus_id_" + k).value = value; } else { document.getElementById("bonus_id_" + i).value = 0; } } } else { $(obj).css('border', '1px solid #ddd'); $(obj).siblings().removeClass("checkbox_frame"); $(obj).siblings().css('border', '1px solid #ddd'); $(obj).siblings().css('pointer-events', 'auto');  document.getElementById("bonus_id_" + k).value = 0; document.getElementById("no_overlap").style.display = "none"; } }  if (is_overlap != 0) { if ($(obj).hasClass("checkbox_frame")) { $(obj).css('border', '1px solid red'); document.getElementById("bonus_id_" + k).value = value; } else { $(obj).css('border', '1px solid #ddd'); document.getElementById("bonus_id_" + k).value = 0; } }  var bonus_ids = ""; for (var j = 0; j < num.length; j++) { if (document.getElementById("bonus_id_" + j).value != 0) { bonus_ids += document.getElementById("bonus_id_" + j).value + ","; } } if(bonus_ids.length>0) { bonus_ids = bonus_ids.substr(0, bonus_ids.length - 1); document.getElementById("bonus_id").value = bonus_ids; } else { document.getElementById("bonus_id").value = 0; } Ajax.call('flow.php?step=change_bonus', 'bonus=' + bonus_ids, changeBonusResponse, 'GET', 'JSON'); }


最后将优惠券的ID数组传过去后,计算优惠后的费用,设置优惠券状态(已使用)即可。

后台处理逻辑

 /* 取得订单信息 */ $order = flow_order_info(); $bonus_id = explode(',',$_GET['bonus']); $k = count($bonus_id); $bonus = bonus_info($bonus_id); if ((!empty($bonus) && $bonus[0]['user_id'] == $_SESSION['user_id'])) { $order['bonus_id'] = $_GET['bonus']; } else { $order['bonus_id'] = 0; //$result['error'] = $_LANG['invalid_bonus']; } $order['bonus'] = $k; /* 计算订单的费用 */ $total = order_fee($order, $cart_goods, $consignee,$content); $smarty->assign('total', $total); 

/**
* 取得红包信息
* @param array bonusidid@paramstring b o n u s i d 红 包 i d ∗ @ p a r a m s t r i n g bonus_sn 红包序列号
* @param array 红包信息
*/

function bonus_info($bonus_id, $bonus_sn = '') { $arr = array(); foreach ($bonus_id as $v) { $sql = "SELECT t.*, b.* " . "FROM " . $GLOBALS['ecs']->table('bonus_type') . " AS t," . $GLOBALS['ecs']->table('user_bonus') . " AS b " . "WHERE t.type_id = b.bonus_type_id "; if ($v > 0) { $sql .= "AND b.bonus_id = '$v'"; } else { $sql .= "AND b.bonus_sn = '$bonus_sn'"; } $arr[] = $GLOBALS['db']->getRow($sql); } return $arr; } 


获得订单中的费用信息 order_fee方法中红包的处理逻辑

if (!empty($order['bonus_id'])) { $money = 0; $bonus_id = explode(',',$order['bonus_id']); $bonus = bonus_info( $bonus_id); for($i = 0 ; $i <count($bonus) ; $i++ ){ $money += $bonus[$i]['type_money']; } $total['bonus'] = $money; //使用一或多个红包减去的总价 } $total['bonus_formated'] = price_format($total['bonus'], false); 

原文链接:https://blog.csdn.net/qq124328227/article/details/62046830?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165277499316782246411588%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165277499316782246411588&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-13-62046830-null-null.nonecase&utm_term=%E4%BC%98%E6%83%A0

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
文明发言,共建和谐米科社区
提交
头像

昵称

取消
昵称表情图片