Laravel 11 E-Commerce - Removing Coupon from Cart

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

Step 1: Create a Method in the CartController

First, let's create a method in the CartController for removing the coupon from the cart:


public function remove_coupon_code()
{
session()->forget('coupon');
session()->forget('discounts');
return back()->with('status','Coupon has been removed!');
}

Step 2: Create a Route for the Function

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


Route::delete('/cart/remove-coupon',[CartController::class,'remove_coupon_code'])->name('cart.coupon.remove');

Step 3: Add a Remove Coupon Link

Now, go to the cart.blade.php file and add a remove coupon link inside the cart-footer:


<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>
@else
<form class="position-relative bg-body" method="POST" action="{{route('cart.coupon.remove')}}">
@csrf
@method('DELETE')
<input class="form-control text-success fw-bold" type="text" name="coupon_code" placeholder="Coupon Code" value="{{session()->get('coupon')['code']}} Applied!" readonly>
<input class="btn-link fw-medium position-absolute top-0 end-0 h-100 px-4 text-danger" type="submit" value="REMOVE COUPON">
</form>
@endif
<form class="position-relative bg-body" method="POST" action="{{route('cart.empty')}}">
@csrf
@method('DELETE')
<button class="btn btn-light" type="submit">CLEAR CART</button>
</form>
</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. The cart total will be updated accordingly.

Now, let's remove the coupon. Click on the "Remove Coupon" link, and you will see that the coupon has been removed. The cart total will be updated again to reflect the removal of the coupon.

In this way, you can remove a coupon from the cart.

That's all about removing a coupon from the cart.