samedi 18 juin 2022

I have a problem when adding the coupon to get a discount

I tried checking many times but it always shows the message that Coupon Code is invalid!

//cart-component.blade.php

            <div class="summary">
                    <div class="order-summary">
                        <h4 class="title-box">Order Summary</h4>
                        <p class="summary-info"><span class="title">Subtotal</span><b class="index">LE</b></p>
                        @if(Session::has('coupon'))
                        <p class="summary-info"><span class="title">Discount ()</span><b class="index">LE </b></p>
                        <p class="summary-info"><span class="title">Subtotal With Discount</span><b class="index">LE </b></p>
                        <p class="summary-info"><span class="title">Tax (%)</span><b class="index">LE </b></p>
                        <p class="summary-info total-info "><span class="title">Total</span><b class="index">LE </b></p>

                        @else
                        <p class="summary-info"><span class="title">Tax</span><b class="index">LE</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">LE</b></p>
                        @endif

                    </div>
                    @if(!Session::has('coupon'))
                        <div class="checkout-info">
                            <label class="checkbox-field">
                                <input class="frm-input " name="have-code" id="have-code" value="1" type="checkbox" wire:model="haveCouponCode"><span>I have a 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"></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="checkout.html">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>
                    <div class="update-clear">
                        <a class="btn btn-clear" href="#" wire:click.prevent="destroyAll()">Clear Shopping Cart</a>
                        <a class="btn btn-update" href="#">Update Shopping Cart</a>
                    </div>
                </div>
    

//And here is the Component class

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Sale;
use App\Models\Coupon;
use Cart;

class CartComponent extends Component
{
    public $haveCouponCode;
    public $couponCode;
    public $discount;
    public $subtotalAfterDiscount;
    public $taxAfterDiscount;
    public $totalAfterDiscount;

    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 calculateDiscount(){
        if(session()->has('coupon')){
            if(session()->get()['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->taxAfterDiscount = $this->subtotalAfterDiscount + $this->taxAfterDiscount;
        }
    }
    public $sale;
    public function render()
    {
        if(session()->has('coupon')){
            if(Cart::instance('cart')->subtotal() < session()->get('coupon')['cart_value']){
                session()->forget('coupon');
            }else{
                $this->calculateDiscount();
            }
        }
        $sale = Sale::find(1);
        return view('livewire.cart-component',['sale'=>$sale])->layout("layouts.base");
    }
}

//cart.php in config file This is the page responsible for the tax

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default tax rate
    |--------------------------------------------------------------------------
    |
    | This default tax rate will be used when you make a class implement the
    | Taxable interface and use the HasTax trait.
    |
    */

    'tax' => 21,

    /*
    |--------------------------------------------------------------------------
    | Shoppingcart database settings
    |--------------------------------------------------------------------------
    |
    | Here you can set the connection that the shoppingcart should use when
    | storing and restoring a cart.
    |
    */

    'database' => [

        'connection' => null,

        'table' => 'shoppingcart',

    ],

    /*
    |--------------------------------------------------------------------------
    | Destroy the cart on user logout
    |--------------------------------------------------------------------------
    |
    | When this option is set to 'true' the cart will automatically
    | destroy all cart instances when the user logs out.
    |
    */

    'destroy_on_logout' => false,

    /*
    |--------------------------------------------------------------------------
    | Default number format
    |--------------------------------------------------------------------------
    |
    | This defaults will be used for the formated numbers if you don't
    | set them in the method call.
    |
    */

    'format' => [

        'decimals' => 2,

        'decimal_point' => '.',

        'thousand_seperator' => ','

    ],

];

What is the problem with my code? I tried many times and checked the code, but I did not find a solution



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/8Eke7f9
via IFTTT

Aucun commentaire:

Enregistrer un commentaire