【问题标题】:Shopify AJAX - update total price of line itemShopify AJAX - 更新订单项的总价
【发布时间】:2018-06-25 18:57:56
【问题描述】:

我在购物车页面上找到了如何通过 AJAX 更改您的商品数量,我还找到了如何通过 AJAX 更新购物车总数。

我还没有找到如何在您增加或减少数量时更新每个订单项的总订单项价格。

这是我到目前为止的代码(不工作)...

jQuery.getJSON('/cart.js', function(cart) { $('#line-total-{{ item.id }}').html(Shopify.formatMoney(item.line_price).replace('$','£'))})

关于我的代码有什么问题的任何提示?

如果有帮助,这也可以在按钮上单击。

【问题讨论】:

    标签: ajax shopify


    【解决方案1】:

    您的代码包括:

    jQuery.getJSON('/cart.js', function(cart) { 
      $('#line-total-{{ item.id  }}').html(Shopify.formatMoney(item.line_price).replace('$','£'))})
    

    上述方法不起作用,因为基于 Liquid 的 {{ item.id }} 将被放入页面一次,之后不再更改。 (很可能,上面的代码包含在一个没有名为 item 的 Liquid 变量的地方,所以渲染的 Javascript 会简单地读取 $('#line-total-')

    您需要使用为您的函数提供的 Javascript cart 对象。例如:

    jQuery.getJSON('/cart.js', function(cart) { 
      for(var i=0; i<cart.items.length; i++){
        var item = cart.items[i];
        var price_element = $('#line-total-' + item.id)
        price_element.html(Shopify.formatMoney(item.line_price).replace('$','£'))})
      }
    })
    

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      您正在从 /cart.js 返回“购物车”,但您没有读取它的任何数据。

      您需要在您的 javascript 中通读购物车对象(将表示为 shopify "Cart" object)。我看到您正在尝试获取“item.id”和“item.line_price”,但与商品和购物车对象没有关系。此时您也不能使用液体,因为它已经呈现给客户端...

      您可能需要执行以下操作:

      jQuery.getJSON('/cart.js', function(cart) { cart.items.each($('#line-total-'+this.id).html(Shopify.formatMoney(this.line_price).replace('$','£')););});
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-25
        • 2012-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-06
        • 2015-12-13
        相关资源
        最近更新 更多