Stars
Stars

Reputation: 55

How to remove product from cart

I have a question about javascript. I have a cart and a function which removes a product from that cart. How can I do a redirect to the main page when the cart is empty?

This is my function delete product.

function removeCart(key) {
$.ajax({
    url: 'index.php?route=checkout/cart/update',
    type: 'post',
    data: 'remove=' + key,
    dataType: 'json',
    success: function(json) {
        $('.success, .warning, .attention, .information').remove();

        if (json['output']) {
            $('#cart_total').html(json['total']);
            $("table.total tr:last td:last").text(json['total'].split('-')[1]);

            $('#cart .content').html(json['output']);
        }           
    }
});
}

Upvotes: 0

Views: 1664

Answers (1)

Chris Laarman
Chris Laarman

Reputation: 1589

You should get your cart when you do the update. If the cart is empty you can return that in your ajax response. For example by setting an emptyCart key in your array:

function removeCart(key) {
$.ajax({
    url: 'index.php?route=checkout/cart/update',
    type: 'post',
    data: 'remove=' + key,
    dataType: 'json',
    success: function(json) {
        $('.success, .warning, .attention, .information').remove();
        if (json['emptyCart']) {
            location.href="/where-you-want-it-to-go";
        }
        if (json['output']) {
            $('#cart_total').html(json['total']);
            $("table.total tr:last td:last").text(json['total'].split('-')[1]);

            $('#cart .content').html(json['output']);
        }
    }
});
}

Upvotes: 1

Related Questions