LaravelShoppingcart
LaravelShoppingcart copied to clipboard
How to return the cart:count when using ajax
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()
Related discussion https://github.com/bumbummen99/LaravelShoppingcart/issues/90