Laravel 11 E-Commerce - Admin Implement Coupon on Cart

Welcome back to the Laravel E-Commerce Project Tutorial. In this video, we will learn about implementing a coupon on the cart.

Step 1: Create a Method in the CartController

First, let's create a method in the CartController for applying the coupon:


public function apply_coupon_code(Request $request)
{
$coupon_code = $request->coupon_code;
if(isset($coupon_code))
{
$coupon = Coupon::where('code',$coupon_code)->where('expiry_date','>=',Carbon::today())->where('cart_value','<=',Cart::instance('cart')->subtotal())->first();
if(!$coupon)
{
return back()->with('error','Invalid coupon code!');
}
session()->put('coupon',[
'code' => $coupon->code,
'type' => $coupon->type,
'value' => $coupon->value,
'cart_value' => $coupon->cart_value
]);
$this->calculateDiscounts();
return back()->with('status','Coupon code has been applied!');
}
else{
return back()->with('error','Invalid coupon code!');
}
}

And:



public function calculateDiscounts()
{
$discount = 0;
if(session()->has('coupon'))
{
if(session()->get('coupon')['type'] == 'fixed')
{
$discount = session()->get('coupon')['value'];
}
else
{
$discount = (Cart::instance('cart')->subtotal() * session()->get('coupon')['value'])/100;
}

$subtotalAfterDiscount = Cart::instance('cart')->subtotal() - $discount;
$taxAfterDiscount = ($subtotalAfterDiscount * config('cart.tax'))/100;
$totalAfterDiscount = $subtotalAfterDiscount + $taxAfterDiscount;

session()->put('discounts',[
'discount' => number_format(floatval($discount),2,'.',''),
'subtotal' => number_format(floatval(Cart::instance('cart')->subtotal() - $discount),2,'.',''),
'tax' => number_format(floatval((($subtotalAfterDiscount * config('cart.tax'))/100)),2,'.',''),
'total' => number_format(floatval($subtotalAfterDiscount + $taxAfterDiscount),2,'.','')
]);
}
}

Step 2: Create a Route for the Function

Next, go to the web.php file and create a route for the apply_coupon_code function:



Route::post('/cart/apply-coupon',[CartController::class,'apply_coupon_code'])->name('cart.coupon.apply');

Step 3: Add a Coupon Form

Now, go to the cart.blade.php file and add a coupon form. Before the form, add an if directive to check if a coupon exists in the session as follows:


<div class="cart-table-footer">
@if(!Session::has("coupon"))
<form class="position-relative bg-body" method="POST" action="{{route('cart.coupon.apply')}}">
@csrf
<input class="form-control" type="text" name="coupon_code" placeholder="Coupon Code">
<input class="btn-link fw-medium position-absolute top-0 end-0 h-100 px-4" type="submit" value="APPLY COUPON">
</form>
@endif
</div>

Step 4: Display the Discounted Cart Total

After applying the coupon, let's display the discounted cart total. In the cart.blade.php file, before the cart total, add an if directive to check if the session has a discount:


<div class="shopping-cart__totals-wrapper">
<div class="sticky-content">
<div class="shopping-cart__totals">
<h3>Cart Totals</h3>
@if(Session::has('discounts'))
<table class="cart-totals">
<tbody>
<tr>
<th>Subtotal</th>
<td>${{Cart::instance('cart')->subtotal()}}</td>
</tr>
<tr>
<th>Discount {{Session("coupon")["code"]}}</th>
<td>-${{Session("discounts")["discount"]}}</td>
</tr>
<tr>
<th>Subtotal After Discount</th>
<td>${{Session("discounts")["subtotal"]}}</td>
</tr>
<tr>
<th>SHIPPING</th>
<td class="text-right">Free</td>
</tr>
<tr>
<th>VAT</th>
<td>${{Session("discounts")["tax"]}}</td>
</tr>
<tr class="cart-total">
<th>Total</th>
<td>${{Session("discounts")["total"]}}</td>
</tr>
</tbody>
</table>
@else
<table class="cart-totals">
<tbody>
<tr>
<th>Subtotal</th>
<td>${{Cart::instance('cart')->subtotal()}}</td>
</tr>
<tr>
<th>SHIPPING</th>
<td class="text-right">Free</td>
</tr>
<tr>
<th>VAT</th>
<td>${{Cart::instance('cart')->tax()}}</td>
</tr>
<tr class="cart-total">
<th>Total</th>
<td>${{Cart::instance('cart')->total()}}</td>
</tr>
</tbody>
</table>
@endif
</div>
<div class="mobile_fixed-btn_wrapper">
<div class="button-wrapper container">
<a href="{{route('cart.checkout')}}" class="btn btn-primary btn-checkout">PROCEED TO CHECKOUT</a>
</div>
</div>
</div>
</div>

Now it's done! Let's check it out. Add some products to the cart, apply the coupon code "OFF10", and you will see that the coupon has been applied.

In this way, you can implement a coupon on the cart.

That's all about implementing a coupon on the cart.