LaravelShoppingcart icon indicating copy to clipboard operation
LaravelShoppingcart copied to clipboard

How to return the cart:count when using ajax

Open johnef opened this issue 4 years ago • 1 comments

I am using Ajax to add products to cart

here is my Ajax code

<script>
jQuery(document).ready(function () {
    jQuery('#addToCart{{$product->id}}').click(function (e) {
        e.preventDefault();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        let package_id = document.querySelector('input[name="package"]:checked').value;
        let pro_id ={{$product->id}};
        let quantity = jQuery('#quantity{{ $product->id }}').val();
        jQuery.ajax({
            url: "{{ route('add.to.cart') }}",
            method: 'post',
            data: {
                _token: $('meta[name="_token"]').attr('content'),
                package: package_id,
                pro_id: pro_id,
                quantity: quantity,
            },
            success: function (data) {
                console.log({{ Cart::count() }})
            },
            error: function (xhr, status, error) {
                console.log(xhr, status, error)
            },
        })
    })
})
</script>

I need to update the Cart::count()

johnef avatar Jun 11 '20 19:06 johnef

Related discussion https://github.com/bumbummen99/LaravelShoppingcart/issues/90

bumbummen99 avatar Jun 11 '20 21:06 bumbummen99