我尝试应用优惠券代码时遇到验证问题。 我必须在 4 条件
下申请以下是该功能的控制器部分:-
public function checkCoupon(Request $res)
{
$code = $res->code;
$check = DB::table('coupons')
->where('coupon_code',$code)
->get();
if(count($check)=="1") {
$user_id = Auth::user()->id;
$check_used = DB::table('used_coupons')
->where('user_id', $user_id)
->where('coupon_id', $check[0]->id)
->count();
if($check_used=="0"){
$used_add = DB::table('used_coupons')
->insert([
'coupon_id' => $check[0]->id,
'user_id' => $user_id
]);
$insert_cart_total = DB::table('cart_total')
->insert([
'cart_total' => Cart::total(),
'discount' => $check[0]->discount,
'user_id' => $user_id,
'gtotal' => Cart::total() - (Cart::total() * $check[0]->discount)/100,
]);
}
else{
?>
<div class="alert alert-warning">This Coupon Have Already Used</div>
<?php
}
}
else if($code==$check){
?>
<div class="alert alert-danger">Wrong Coupon Code Entered</div>
<?php }
else{
?>
<div class="alert alert-danger">Please Insert The Coupon Code </div>
<?php }
}
JS 文件用于应用优惠券功能按钮:-
$(document).ready(function(){
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
data: 'code=' + coupon_id,
success:function(res){
$('#cartTotal').html(res);
}
})
});
});
查看文件
<div class="cart-total" >
<h4>Total Amount</h4>
<table>
<tbody>
<tr>
<td>Sub Total</td>
<td>$ <?php echo Cart::subtotal(); ?></td>
</tr>
<tr>
<td>Tax (%)</td>
<td>$ <?php echo Cart::tax(); ?></td>
</tr>
<tr>
<td>Grand Total new</td>
<td>$ <?php echo Cart::total(); ?></td>
</tr>
<tr>
<td>Discount(%) </td>
<td> <?php echo $disnew; ?></td>
</tr>
<tr>
<td>Grand Total (After discount) </td>
<td>$ <?php echo $gtnew; ?></td>
</tr>
</tbody>
</table>
<input type="submit" class="btn update btn-block" style="color: white;font-weight: bold;" value="Continue Shopping">
<a href="<?php echo url('checkout') ?>" class="btn check_out btn-block" style="color: white;font-weight: bold;">Checkout</a>
</div>
答案 0 :(得分:5)
要更好地检查已使用的优惠券,以使用 exists (而不是计数)。
要获得优惠券,最好使用 first 而不是 get (首先-返回第一个元素,您无需在集合中使用索引)
我认为,在这种情况下最好使用收益。代码和逻辑看起来很清楚。
public function checkCoupon(Request $res)
{
$code = $res->input('code');
$userId = Auth::user()->id;
if(!$code){
return ['error' => '<div class="alert alert-danger">Please Insert The Coupon Code </div>'];
}
$coupon = DB::table('coupons')
->where('coupon_code', $code)
->first();
if(!$coupon) {
return ['error' => '<div class="alert alert-danger">Wrong Coupon Code Entered</div>'];
}
$isUsed = DB::table('used_coupons')
->where('user_id', $userId)
->where('coupon_id', $coupon->id)
->exists();
if($isUsed){
return ['error' => '<div class="alert alert-warning">This Coupon Have Already Used</div>'];
}
DB::table('used_coupons')
->insert([
'coupon_id' => $coupon->id,
'user_id' => $userId
]);
DB::table('cart_total')
->insert([
'cart_total' => Cart::total(),
'discount' => $coupon->discount,
'user_id' => $userId,
'gtotal' => Cart::total() - (Cart::total() * $coupon->discount)/100,
]);
return [
'subtotal' => Cart::subtotal(),
'total' => Cart::total()
];
}
jQuery:
$(document).ready(function(){
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
dataType: "json",
data: {code: coupon_id},
success:function(res){
if(res.error){
$('...error_selector...').html(res.error);
} else {
$('...total_selector...').html(res.total);
$('...subtotal_selector...').html(res.subtotal);
}
}
})
});
});