简体   繁体   English

实施购物车JavaScript

[英]Implement shopping cart JavaScript

here what I am trying to do is when a user clicks the + button function should get called, it looks for the cookie shopping_cart. 在这里,我试图做的是当用户单击+按钮函数时,它将查找cookie shopping_cart。 Then it tries to find the JSON with key 'item_qty', which is key-value pair of all the items in the cart. 然后,它尝试查找具有键“ item_qty”的JSON,该键是购物车中所有商品的键值对。 But the cart is not updating, moreover when clicked on + button is showing Unexpected token N in JSON at position 0 但是购物车没有更新,此外,当单击+按钮时,在位置0的JSON中显示意外令牌N

In browser console I am getting it as 在浏览器控制台中,我得到它

"csrftoken=some_value; shopping_cart=None" “ csrftoken = some_value; shopping_cart = None”

var updateCart = function (count) {
    $('#cart-info').val($('#cart-info').val() + count);
};



var item_add = function (item_slug) {
    var shopping_cart = JSON.parse($.cookie("shopping_cart"));
    var item_slug = item_slug;
    if(shopping_cart.hasOwnProperty('item_qty')){
        item_qty_dict = shopping_cart['item_qty'];
        if(item_qty_dict.hasOwnProperty(item_slug)){
            var count_pre = item_qty_dict[item_slug];
            item_qty_dict[item_slug] = count_pre + 1;
        }
        else {
            item_qty_dict[item_slug] = 1;
            shopping_cart['item_qty'] = item_qty_dict;
        }
    }
    else {
        shopping_cart = {}
        shopping_cart['item_qty'] = {item_slug: 1};
    }
    $.cookie("shopping_cart", JSON.stringify(shopping_cart));
    var temp= $.cookie('shopping_cart')
    console.log(JSON.parse(temp));
};


var buttonPlus = $(".cart-qty-plus");

var incrementPlus = buttonPlus.click(function () {
    var $n = $(this)
        .parent(".qnty_chngr")
        .find(".qty");
    $n.val(Number($n.val()) + 1);
    var product_slug = $(this).parent(".qnty_chngr").siblings('.product-slug').val();
    console.log(product_slug);
    updateCart(1);
    item_add(product_slug);
});

HTML: HTML:

                        <div class="qnty_chngr">
                                    <button class="cart-qty-plus" type="button" value="+">+</button>
                                    <input type="text" name="qty" maxlength="12" value="1" class="input-text qty"/>
                                    <button class="cart-qty-minus" type="button" value="-" title="Add less quantity">-</button>
                        </div>
                        <input type="hidden" class="product-slug" name="product_slug" value="{{ medicine.slug }}">
                        <div class="add_to_cart">
                            <button  class="add_to_cart_txt" value="10"><span class="AddInfoBtn">Add </span></button>

                        </div>,

Seems in your cookie there is no valid JSON. 您的Cookie中似乎没有有效的JSON。 None is not valid, it should be "None" (with quotes). None无效,应为"None" (带引号)。 Remove the cookie before testing. 在测试之前删除cookie。

Also if possible try using this library which works the way you expected. 另外,如果可能,请尝试使用符合您预期方式的该库。

https://github.com/js-cookie/js-cookie https://github.com/js-cookie/js-cookie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM