Header Ads

Header ADS

Laravel ajax add to wishlist product

  <a aria-label="Add to Wishlist" class="action-btn" id="{{$best_sell->id}}"
onclick="addToWishlist(this.id)"><i class="fa fa-heart"
style="font-size: 19px; text-align:center; color:#fff; margin-top:-3px;"
aria-hidden="true"></i></a>

 Route::post('/add-to-wishlist/{product_id}', 'addWishList');

  //Product Wishlist By User
      public function addWishList(Request $request, $product_id)
      {
        if(Auth::check()){

            $exists = Wishlist::where('user_id', Auth::id())->where('product_id',$product_id)->first();

            if(!$exists){
                Wishlist::insert([
                    'user_id' =>Auth::id(),
                    'product_id' =>$product_id,
                    'created_at'  => Carbon::now(),
                ]);
                return response()->json([
                    'success' => 'Successfully added this Product on your Wishlist'
                ]);
            }else{
                return response()->json([
                    'error' => 'This Property has already in your Wishlist'
                ]);
            }
        }else{
            return response()->json([
                'error' => 'At first login your account'
            ]);
        }

      } //end method

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    {{-- toastr js --}}
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>

    {{-- sweetalert js --}}
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>

    <script>
     @if(Session::has('message'))
     var type = "{{ Session::get('alert-type','info') }}"
     switch(type){
        case 'info':
        toastr.info(" {{ Session::get('message') }} ");
        break;
   
        case 'success':
        toastr.success(" {{ Session::get('message') }} ");
        break;
   
        case 'warning':
        toastr.warning(" {{ Session::get('message') }} ");
        break;
   
        case 'error':
        toastr.error(" {{ Session::get('message') }} ");
        break;
     }
     @endif
    </script>

    {{-- add to Wishlist property   --}}

    <script type="text/javascript">

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
   
        function addToWishlist(product_id){
   
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: "/add-to-wishlist/"+product_id,
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                success: function(data){
   
                            const Toast = Swal.mixin({
                            toast: true,
                            position: 'top-end',
                           
                            showConfirmButton: false,
                            timer: 3000
                        })
                        if ($.isEmptyObject(data.error)) {
                               
                                Toast.fire({
                                type: 'success',
                                icon: 'success',
                                title: data.success,
                                })
   
                        }else{
                       
                    Toast.fire({
                                type: 'error',
                                icon: 'error',
                                title: data.error,
                                })
                            }
   
                }
   
            });
   
        }
   
 </script>




No comments

Theme images by fpm. Powered by Blogger.