【问题标题】:How to remove product from cart如何从购物车中删除产品
【发布时间】:2012-09-18 09:30:48
【问题描述】:

我有一个关于 javascript 的问题。我有一个购物车和一个从该购物车中删除产品的功能。购物车为空时如何重定向到主页?

这是我的功能删除产品。

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']);
        }           
    }
});
}

【问题讨论】:

  • 我知道,但我什么时候必须这样做?当购物车是空的时我怎么知道这一点
  • @Stars 只检查是否没有产品。
  • 移除产品似乎不是这里的问题,仅在您的购物车为空时检查。为了帮助您,我们需要了解您如何构建购物车。
  • 总存储是什么?如果这是计数或总值,ypu 可以检查是否为零然后重定向
  • @Stars 既然这是你的软件,谁知道如何检查购物车是否是空的。或者您使用的是已知的商店软件?如果是,你应该给它命名。

标签: javascript html json shopping-cart


【解决方案1】:

您应该在更新时拿到您的购物车。如果购物车是空的,您可以在 ajax 响应中返回它。例如,通过在数组中设置一个 emptyCart 键:

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']);
        }
    }
});
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-17
    • 2022-08-23
    • 1970-01-01
    • 2022-06-16
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多