如何检查产品是否已经在购物车中

时间:2019-12-10 13:54:29

标签: javascript jquery

我有一个网上商店,用户只能订购1件/产品。我需要检查该用户是否已经在购物车中拥有相同的产品,如果是,请发出警报,不要再次将该产品添加到购物车中。如果产品不在购物车中,请将其添加到购物车中。 这是我首先尝试的方法,但是它始终将产品添加到购物车,即使If是否为真。问题是购物车。

$(".glyphicon-shopping-cart").on("click", function() {
    $(this).css("color", "#29d646");
    var BookID = $(this).parent().parent().prev().prev().prev().prev().prev().prev().text();
    $.ajax({
        type: 'GET',
        url: '/api/selections/' + BookID,
        dataType: 'json',
        headers: {
            'Authorization': 'Bearer ' + localStorage.getItem('accessToken')
        },
        success: function(sel) {
            //check if Book is in cart already
            $.ajax({
                type: 'GET',
                url: '/api/carts',
                dataType: 'json',
                success: function(cart) {
                    if (cart.CompanyID == JSON.parse(localStorage.getItem('userName')) && cart.ISBN == sel.ISBN) {
                        alert('Product is already in cart');
                    } else {
                        var toCart = {
                            CompanyID: JSON.parse(localStorage.getItem('userName')),
                            Orderdate: getdate(),
                            ISBN: sel.ISBN,
                            BookName: sel.BookName,
                            Author: sel.Author,
                            Publisher: sel.Publisher,
                            Price: sel.Price,
                            Season: sel.Season,
                            IsInCart: true,
                        };
                        $.ajax({
                            type: 'post',
                            url: '/api/Carts',
                            data: toCart,
                            success: function() {
                                console.log('Added to cart ' + BookID);
                                location.reload();
                            },
                            error: function() {
                                alert('Virhe!');
                            }
                        });
                    }
                }
            });
        }
    });
});

这是我尝试执行的另一种方法,现在IF-语句正在工作,但仍将产品添加到购物车。我该怎么办?

//check if Book is in cart already
$.ajax({
    type: 'GET',
    url: '/api/carts',
    dataType: 'json',
    success: function(cart) {
        $.each(cart, function(i, carts) {
            if (carts.CompanyID == JSON.parse(localStorage.getItem('userName')) && carts.ISBN == sel.ISBN) {
                alert('Product is already in cart');
            }
        })
        var toCart = {
            CompanyID: JSON.parse(localStorage.getItem('userName')),
            Orderdate: getdate(),
            ISBN: sel.ISBN,
            BookName: sel.BookName,
            Author: sel.Author,
            Publisher: sel.Publisher,
            Price: sel.Price,
            Season: sel.Season,
            IsInCart: true,
        };
        $.ajax({
            type: 'post',
            url: '/api/Carts',
            data: toCart,
            success: function() {
                console.log('Added to cart ' + BookID);
                location.reload();
            },
            error: function() {
                alert('Virhe!');
            }
        });
    }
});
}

我不知道这是否是正确的方法,如果有人有更好的解决方案,请告诉我。

2 个答案:

答案 0 :(得分:0)

var present=false;
$.each(cart, function(i, carts) {
if (carts.CompanyID == JSON.parse(localStorage.getItem('userName')) && 
carts.ISBN == sel.ISBN) {
alert('Product is already in cart');
present=true;
}
})
if(present===false){
other ajax part
}

答案 1 :(得分:0)

您可以通过添加return立即退出该功能。它会“尽早退出”该功能,并阻止程序继续添加购物车。

if (...) {
  alert('Product is already in cart');
  return;
}