【问题标题】:Shopify cart.item_count not updating using AJAX onclick functionShopify cart.item_count 未使用 AJAX onclick 功能更新
【发布时间】:2020-05-14 17:26:39
【问题描述】:

我正在尝试更新 Shopify 购物车 onclick。它正在工作,但是当我尝试使用购物车添加 success: function 更新购物车计数而不刷新页面时,它不起作用。下面是我的代码。

$(function(){
    $('.varients-item').on('click', function(){
        var obj = $(this);
        $.ajax({
            type: 'POST',
            url: '/cart/add.js',
            data: {
                quantity: 1,
                id: $(this).attr("data-variant")
            },
            dataType: 'json',
            success: function (data) {  
                //alert('Item added to cart');
                $('.cart-item-count').html(cart.item_count);
            }
        });
    });
});

这是console 错误:

【问题讨论】:

    标签: jquery ajax shopify liquid


    【解决方案1】:

    这是我的解决方案。

    $(function(){
        var cartCount = {{ cart.item_count }};
        $('.varients-item').on('click', function(){
            var obj = $(this);
            $.ajax({
                type: 'POST',
                url: '/cart/add.js',
                data: {
                    quantity: 1,
                    id: $(this).attr("data-variant")
                },
                dataType: 'json',
                success: function (data) {
                    $.ajax({
                        type: 'GET',
                        dataType: 'json',
                        url: '/cart.json',
                        success: function(cart){
                            cartCount++;
                            $('.cart-item-count').html(cartCount);
                        }
                    });
                }
            });
        });
    });
    

    【讨论】:

      【解决方案2】:

      您正在调用cart.item_count,但未定义cart。它写在错误响应中。

      您在此处将data 作为回调参数传递给success: function (data),因此您应该调用data.item_count

      但是 cart/add.js 不返回购物车的 JSON,而是返回添加的产品 JSON。您需要向cart.js 发出第二次请求才能获取购物车对象!

      所以你的代码应该是这样的:

      $(function(){
        $('.varients-item').on('click', function(){
            var obj = $(this);
            $.ajax({
                type: 'POST',
                url: '/cart/add.js',
                data: {
                    quantity: 1,
                    id: $(this).attr("data-variant")
                },
                dataType: 'json',
                success: function () {  
                    $.ajax({
                      url: '/cart.js',
                      success: function(cart){
                        $('.cart-item-count').html(cart.item_count);
                      }
                    })
                }
            });
        });
      });
      

      【讨论】:

      • 我添加了你的代码但是发现了新的错误prntscr.com/sgvkxj
      • 。我的 jQuery 版本3.4.1。当我替换为1.12.4 时没有错误,但购物车计数未在点击时更新。
      • 您的解决方案不适合我。但是你给了我如何解决的线索。以下是我的回答。
      • 解决方案可能有效,但您需要像为 POST 函数一样为 GET 添加 json 格式参数(dataType: 'json',)以避免错误。
      • @AliceGirard 在这种情况下,您不需要传递 dataType,因为我们正在请求 JS 路由,jQuery ajax 会自动理解我们期望 JSON 作为响应。请参阅此处的文档:api.jquery.com/jquery.ajax,其中声明可以猜测默认响应类型。如果我们请求一个带有 JSON 响应的 HTML 页面,那就另当别论了。
      猜你喜欢
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多