I am using jquery and ajax to add stuff to cart and to remove them, so now I have created code remove but after I have click remove its removing but not refreshing the cart. I want this to be done using ajax to refresh the cart. Below is my code for adding, loading and removing from cart
I want this to be done using ajax to refresh the cart. Below is my code for adding, loading and removing from cart
$(document).ready(function() {
load_cart_data();
function load_cart_data() {
$.ajax({
type: "get",
url: "/fetch_cart/cart",
dataType: "json",
cache: false,
success: function(data) {
$("#cart-sidebar").html(data.cart_details);
$(".total_price").text(data.total_price);
$(".total_items").text(data.total_items);
}
});
}
//add to cart
$('.add-to-cart').click(function() {
var id = $(this).attr('id');
var quantity = $('#quantity' + id).val();
$.ajax({
type: "GET",
url: "/add_to_cart/" + id + "/" + quantity,
data: id,
quantity,
cache: false,
success: function(data) {
load_cart_data();
alert(data);
}
});
});
$(document).on('click', '.remove-cart', function() {
var id = $(this).attr('id');
$.ajax({
type: "GET",
url: "/remove_from_cart" + "/" + id,
data: id,
cache: false,
success: function(data) {
load_cart_data();
alert(data);
}
});
});
});