KACKbIP
4 months ago

Hi Guys I need help

Posted 4 months ago by KACKbIP

I want to make increment and decrement cart without loading a page. My Javascript(Ajax)

    <script type="text/javascript">

        $.ajaxSetup({

            headers: {

                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')

            }

        });

        $(".increment").click(function(e){

            e.preventDefault();

            var proid = $("input[name=proid]").val();

            var quantity = $("input[name=quantity]").val();



            $.ajax({

                type:'POST',

                url:'one/product/cart',

                data:{proid:proid, quantity:quantity},

             

            });



        });

    </script>

my route

Route::post('one/product/cart', '[email protected]')->name('adding.product');

My view

  <div class="row row-count-cart">
    <form method="POST" action="{{ route('adding.product') }}">
        {{ csrf_field() }}
        <div class="small-6 column">
        <input type="hidden" value="{{ $pro->id }}" name="proid">
            <div class="product-count">
                <div class="input-group number-spinner">
                    <div class="input-group-button input-group-btn">
                        <button class="button decrement " type="button" name="decr"  data-dir="dwn" value="true">-</button>
                    </div>
                        <input class="input-group-field" type="number" id="bx_3966226736_2425_quantity" name="quantity" value="{{ \Cart::get($pro->id) ? \Cart::get($pro->id)->quantity : 1 }}" min="1">
                    <div class="input-group-button">
                    <button  class="button increment " type="button" name="inc"  data-dir="up" value="true">+</button>
                    </div>
                </div>
            </div>
        </div>
    <div id="bx_3966226736_2808_basket_actions" class="small-6 columns">
         <button  id="bx_3966226736_2808_buy_link" href="" class="button tiny add2cart" data-preview="#bx_3966226736_2808_pict" data-product-id="2808" type="submit"><span>Купить</span></button>
    </div>
</form>
</div>

my controller

 public function addingOneProduct(Request $request)
    {

        if(isset($request->inc)) {
            if(empty(\Cart::get($request->proid))) {
                $product = Product::where('id', $request->proid)->first();

                \Cart::add(
                    array(
                        'id' => $product->id,
                        'name' => $product->name,
                        'price' => $product->price,
                        'quantity' => 2,
                        'attributes' => array(
                            'image' => $product->image,
                            'discount' => $product->discount,
                        ),

                    ));

                return redirect()->back();
            } else {
                $product = Product::where('id', $request->proid)->first();

                \Cart::update($product->id, array(
                    'name' => $product->name,
                    'price' => $product->price,
                    'quantity' => 1,
                    'attributes' => array(
                        'image' => $product->image,
                        'discount' => $product->discount,
                    ),
                ));

                return redirect()->back();
            }
            
        }
        else if(isset($request->decr))
        {
            $product = Product::where('id', $request->proid)->first();

            \Cart::update($product->id, array(
                'name' => $product->name,
                'price' => $product->price,
                'quantity' => -1,
                'attributes' => array(
                    'image' => $product->image,
                    'discount' => $product->discount,
                ),
            ));


            return redirect()->action('[email protected]');
        }


    }

it works fine without Javascript, but I need to do it without refreshing the page. I can't get incremented and decremented values

Please sign in or create an account to participate in this conversation.