【问题标题】:Show product quantity in cart in products div with ajax使用ajax在产品div中显示购物车中的产品数量
【发布时间】:2014-12-10 07:46:39
【问题描述】:

我正在为我的 Shopify 商店使用默认的 Supply 主题,并且我已启用 Ajaxify 购物车选项。

我已经自定义了主题,因此如果客户将产品 X 添加到购物车,重新加载并将鼠标悬停在产品上(在收藏页面上),它会显示产品 X 已添加到购物车 1 次。

这是显示购物车中产品 X 数量的液体代码:

<!-- Snippet from product-grid-item.liquid -->

{% assign count = 0 %}
{% for item in cart.items %}
  {% if product.id == item.product.id %}
    {% assign count = count | plus: item.quantity %}
  {% endif  %}
{% endfor %}

...

<span class="item-quantity">{{ count }}</span>

如果产品在购物车 .triangle-top-right 中,则添加到 div#in-cart-indicator,如果不在购物车中,则类别为 not-in-cart

这是一个 GIF,说明了它目前的样子:

![在此处输入图片描述][1]

目前情况:

  1. 将产品 X 添加到购物车 发生什么了:
    • 购物车数量已更新
  2. 重新加载页面 发生什么了:
    • 包含产品 X 的 div 右上角有一个蓝色三角形,表示产品 X 在购物车中
    • 将鼠标悬停在产品 X 上时,会显示产品 X 在购物车中的次数。

我想要什么:

  1. 将产品 X 添加到购物车 发生什么了:
    • 购物车数量已更新
    • 包含产品 X 的 div 右上角有一个蓝色三角形,表示产品 X 在购物车中
    • 将鼠标悬停在产品 X 上时,会显示产品 X 在购物车中的次数。

我试过弄乱ajaxify.js 中的代码,但我缺少 javascript 似乎会破坏事情。

我可以尝试做什么?

【问题讨论】:

  • 嗯,这是几个问题,所以我会尽力帮助。首先,尽管我们昨天讨论了它,但有一种更简单的方法可以在没有循环的情况下获取购物车的总数。 cart.item_count 返回购物车中的商品数量。
  • 其次,您确实需要使用 Ajax 将您的添加到购物车,而无需刷新工作。我使用 jquery 库来访问 cart.js 的功能。您可以在此处查看我对类似问题的回答 stackoverflow.com/questions/26347107/… - 如果需要,我可以解释和提供更多帮助,但您需要熟悉 ajax 以及如何从对 shopify API 的调用中返回数据。
  • @FunkDoc 感谢您的帮助。首先,我不需要返回购物车中的商品数量,我需要知道某个产品被添加到购物车中的次数,所以如果我添加产品 A 一次,产品 B 两次,它应该显示:产品 A:1。产品 B:2,它现在这样做。其次,感谢cart.js(!)的提示,我会调查一下。
  • @FunkDoc 我有一些问题,有什么方法(Skype、Google Hangout)我们可以使用 IM 聊天吗?
  • 我没有视频功能。如果您对 Jquery 有基本的了解并使用 cart.js,您应该能够操作您需要的内容。当我有一点时间时,我会在下面的答案中添加一些代码,希望能提供更多帮助。

标签: javascript jquery ajax shopify liquid


【解决方案1】:

这是我在用户将鼠标悬停在商店中的购物车图标上时使用的代码。在悬停时,它会获取 cart.js 并使用返回的数据来填写包含购物车信息的数据。我选择不显示购物车悬停中的所有单个项目,因为如果有很多它会变得混乱,但你当然可以。

$("#main-cart-link").hover(function(){
jQuery.getJSON('/cart.js', function (cart, textStatus) {
  if(cart.item_count>0){
    var cartshipnote="";
//if the cart total is close to free shipping add a note
    if(cart.total_price>5000){
      var leftover="$"+((7500-cart.total_price)/100).toFixed( 2 );
      cartshipnote="<div style='padding:4px 0; font-style:italic; text-align:right'>Only "+leftover+" away from free shipping!</div>";
    }
//if the cart total is over free shipping requirement add a note
   if(cart.total_price>7500){
     cartshipnote="<div style='padding:4px 0; font-weight:bold; text-align:right'>You've qualified for free shipping!!</div>";
    }
//show cart total price in US dollars with 2 decimals
  var carttotal="$"+(cart.total_price/100).toFixed( 2 );
//add html to the cart hover div
    jQuery("#ccbody").html("<div id='ccount'>Items in cart: "+cart.item_count+"</div><div id='carttotal'>Total: "+carttotal+"</div>"+cartshipnote); 
  }
});
},function(){
//on hover-out empty the cart div
  jQuery("#ccbody").empty();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    相关资源
    最近更新 更多