In this tutorial we are going to learn about Apply these Coupons on Cart.
So let see how can we apply these coupons on Cart.
In cart page you can see here a check box with text I have a promo code
Now lets change the text here and also add a text box for accepting the coupon code.
For that switch to the project and lets open the cart-component.blade.php view file and the following code.


<div class=\"checkout-info\">
@if(!Session::has('coupon'))
<label class=\"checkbox-field\">
<input class=\"frm-input \" name=\"have-code\" id=\"have-code\" value=\"1\" type=\"checkbox\" wire:model=\"haveCouponCode\"><span>I have coupon code</span>
</label>
@if($haveCouponCode == 1)
<div class=\"summary-item\">
<form wire:submit.prevent=\"applyCouponCode\">
<h4 class=\"title-box\">Coupon Code</h4>
@if(Session::has('coupon_message'))
<div class=\"alert alert-danger\" role=\"danger\">{{Session::get('coupon_message')}}</div>
@endif
<p class=\"row-in-form\">
<label for=\"coupon-code\">Enter your coupon code:</label>
<input type=\"text\" name=\"coupon-code\" wire:model=\"couponCode\" />
</p>
<button type=\"submit\" class=\"btn btn-small\">Apply</button>
</form>
</div>
@endif
@endif
<a class=\"btn btn-checkout\" href=\"#\" wire:click.prevent=\"checkout\">Check out</a>
<a class=\"link-to-shop\" href=\"shop.html\">Continue Shopping<i class=\"fa fa-arrow-circle-right\" aria-hidden=\"true\"></i></a>
</div>


Now go to the CartComponent.php class file and here lets add the following properties.


public $haveCouponCode;
public $couponCode;
public $discount;
public $subtotalAfterDiscount;
public $taxAfterDiscount;
public $totalAfterDiscount;


Now here letes create the following functions one for apply coupon, second for calculate discount and third function for remove coupon.


public function applyCouponCode()
{
$coupon = Coupon::where('code',$this->couponCode)->where('cart_value','<=',Cart::instance('cart')->subtotal())->first();
if(!$coupon)
{
session()->flash('coupon_message','Coupon code is invalid!');
return;
}

session()->put('coupon',[
'code' => $coupon->code,
'type' => $coupon->type,
'value' => $coupon->value,
'cart_value' => $coupon->cart_value
]);

}

public function calculateDiscounts()
{
if(session()->has('coupon'))
{
if(session()->get('coupon')['type'] == 'fixed')
{
$this->discount = session()->get('coupon')['value'];
}
else
{
$this->discount = (Cart::instance('cart')->subtotal() * session()->get('coupon')['value'])/100;
}
$this->subtotalAfterDiscount = Cart::instance('cart')->subtotal() - $this->discount;
$this->taxAfterDiscount = ($this->subtotalAfterDiscount * config('cart.tax'))/100;
$this->totalAfterDiscount = $this->subtotalAfterDiscount +$this->taxAfterDiscount;
}
}

public function removeCoupon()
{
session()->forget('coupon');
}


Now go to the cart-component.blade.php file and make changes inside the order-summary div

<div class=\"order-summary\">
<h4 class=\"title-box\">Order Summary</h4>
<p class=\"summary-info\"><span class=\"title\">Subtotal</span><b class=\"index\">${{Cart::instance('cart')->subtotal()}}</b></p>
@if(Session::has('coupon'))
<p class=\"summary-info\"><span class=\"title\">Discount ({{Session::get('coupon')['code']}}) <a href=\"#\" wire:click.prevent= \"removeCoupon\"><i class=\"fa fa-times text-danger\"></i></a></span></span><b class=\"index\"> -${{number_format($discount,2)}}</b></p>
<p class=\"summary-info\"><span class=\"title\">Subtotal with Discount</span></span><b class=\"index\">${{number_format($subtotalAfterDiscount,2)}}</b></p>
<p class=\"summary-info\"><span class=\"title\">Tax ({{config('cart.tax')}}%)</span></span><b class=\"index\">${{number_format($taxAfterDiscount,2)}}</b></p>
<p class=\"summary-info total-info \"><span class=\"title\">Total</span><b class=\"index\">${{number_format($totalAfterDiscount,2)}}</b></p>
@else
<p class=\"summary-info\"><span class=\"title\">Tax</span><b class=\"index\">${{Cart::instance('cart')->tax()}}</b></p>
<p class=\"summary-info\"><span class=\"title\">Shipping</span><b class=\"index\">Free Shipping</b></p>
<p class=\"summary-info total-info \"><span class=\"title\">Total</span><b class=\"index\">${{Cart::instance('cart')->total()}}</b></p>
@endif
</div>



Now save it and lets check this.
So switch to browser and refresh the page.
And here you can see coupon code textbox is not showing.
Now lets checked this checkbox .
And now you can see the coupon code textbox and this apply button.
Now lets add some product to cart.
In cart you can see one product and cart value is $230.
Now lets try to add any coupon for discount.
So here we have two coupon first is OFF100 which type is fixed it will give $100 discount if cart value is greater than or equal to $1000.
And second coupon which is OFF10 which type is percent it will give 10% discount if cart value is greater than or equal to $1200.
Now lets apply these coupon code on cart.
So go to the cart page and here lets add the coupon code OFF100.
And here you can see here the error message.
Because cart value is not fulfill the condition.
Now lets add more product to cart.
Now lets apply the coupon.
And here you can see coupon has been applied.
Discount -$100.
With discount the subtotal and total price.
Now Lets check the remove coupon.
Now remove this coupon so click here.
And you can see coupon has been removed.
Now lets apply this coupon for percent off.
So type here OFF10 and click on apply.
And you can see this coupon has been applied which is 10% discount.
Now lets remove some product from cart.
And now you can see here coupon is automatically removed due to cart value don’t fulfill the coupon code cart value criteria.
So in this way you can Apply Coupons so that's all about Apply Coupons.