darryldecode / laravelshoppingcart

Shopping Cart Implementation for Laravel Framework
1.34k stars 443 forks source link

quantity increase and decrease and delete sometimes dont work #295

Open raf187 opened 3 years ago

raf187 commented 3 years ago

Hi i make a cart with ajax, sometimes I increase, decrease or delete(especially when i click very fast) and the cart dont save the request !

laravel =>

`public function storeOnDB(Request $request, $id){

    $product = Menu::find($id);
    $extras = ExtrasBoll::find($request->extras);
    if(!$product){
        abort(404);
    }
    $idProd = $product->id . 0 .implode("",$request->extras !== null ? $request->extras : [0]);

    \Cart::session('Cart')->add([
        "id"=>$idProd,
        "name"=>$product->name,
        "price"=>$product->price,
        "quantity"=>1,
        'attributes'=>[
            "prodId"=>$product->id,
            "tva"=>$product->TVA,
            "code"=>$product->code,
            "extra"=>$extras,
        ],
        'associatedModel'=>$product
    ]);

    $cart = \Cart::getContent();

    if($cart->has($idProd)){
        \Cart::update($idProd, [
            'quantity' => [
                'value' => +1
            ]
        ]);
    }
    return redirect()->back();
}

public function deleteFromDB($id){
    \Cart::session('Cart')->remove($id);
    return redirect()->back();
}

public function incraseFromDB($id){
    \Cart::session('Cart')->update($id,[
        'quantity' => +1
    ]);
    return redirect()->back();
}

public function removeFromDB($id){
    \Cart::session('Cart')->update($id,[
        'quantity' => -1
    ]);
    return redirect()->back();
}

public function orderJson(){
    $cart = \Cart::session('Cart')->getContent();
    $totalSupp = 0;
    foreach ($cart as $key=>$value){
        $qty = $value['quantity'];
        $extras = $value['attributes']['extra'];
        if ($extras !== null){
            foreach ($extras as $extra) {
                $price = $extra['price'] === null ? 0 : $extra['price'] * $qty;
                $totalSupp += $price;
            }
        }else{
            $totalSupp += 0;
        }
    }
    $subTotal = \Cart::session('Cart')->getTotal();
    $total = $subTotal + $totalSupp;
    $subQty = \Cart::session('Cart')->getTotalQuantity();
    return compact('cart', 'subQty', 'total');
}`

form =>

` @foreach($bolls as $boll)
 <form id="addCartForm" method="POST" action="{{ '/add-to-cart/'. $boll->id }}">
    <div class="card mb-3">
        <div class="row col-12 no-gutters p-0 m-0">
            <div class="col-lg-3">
                <img src="{{ $boll->url_img }}" class="card-img" alt="Nâga Antibes menu">
            </div>
            <div class="row col-lg-9 px-sm-5 card-body">
                <div class="col-lg-9 px-5 my-auto">
                    <h5 class="card-title text-success font-weight-bold pb-1">{{ $boll->name }}</h5>
                    <p class="card-text">{{ $boll->description }}</p>
                </div>
                <div class="col-lg-3 m-auto text-center addDiv d-flex flex-column">
                    <button type="submit" class="btnPlus display-4 text-success btnOrder"><i class="fas fa-cart-arrow-down"></i></button>
                </div>
 </form>

@endforeach`

and ajax code=>

  `cartBtnAdd(form) {
    let that = this;
    $(document).on('submit', form , function (e) {
        e.preventDefault();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            method: $(this).attr('method'),
            url: $(this).attr('action'),
            data: $(this).serialize(),
            success: function (data){
                if (data) {
                    that.cartJson();
                }
            }
        })
    })
}`

thx

Bobach22 commented 3 years ago

seems you are checking&updating it after adding item with id of $idProd

techn0guy commented 3 years ago

@Bobach22 I'm having this happen to me as well. Did you find a solution? I will update the quantity of a product, fetch the cart data (it will show the updated quantity) and then on refresh the old value will still show. This randomly happens about 1 in 5 times.

frantischek commented 2 years ago

I had the same problem - the I switched to a livewire component and now its working.