简体   繁体   English

检查shopify cart.js中是否存在供应商

[英]Checking if vendor is present in shopify cart.js

I'm trying to find out if a vendor is present within my customers cart using Javascript like so: 我试图找出使用Javascript的客户购物车中是否存在供应商,如下所示:

$.getJSON('/cart.js', function(cart) {

  var inArray = false;
  for (var i = 0; i < cart.length; i++) {
    if (cart[i].vendor === 'example') {
      inArray = true;
      break;
    }
  }
  alert(inArray);

});

If I have a product in my cart from the vendor "example", my alert returns false. 如果我的购物车中有供应商“ example”的产品,我的警报将返回false。 Can anyone shine a light on why? 任何人都可以照亮为什么?

If I do: 如果我做:

$.getJSON('/cart.js', function(cart) {

  $.each(cart.items, function(index, cartItem) {
    alert('This product is ' + cartItem.vendor);
  });

});

I will get an alert with the vendor name for each item in the cart - I just cant seem to check if it exists. 我将收到购物车中每个项目的供应商名称警报,我似乎无法检查它是否存在。

I know I could use liquid to get this information fairly easily, but i'm wanting to restrict delivery days to the jQuery datepicker based on vendors in the cart and current time (on the users machine) so I'm having to use Javascript. 我知道我可以很容易地使用液体来获取此信息,但是我想根据购物车中的供应商和当前时间(在用户计算机上)将jQuery datepicker的交货日期限制为几秒钟,因此我不得不使用Javascript。

I'm surprised your first script even runs. 我很惊讶您的第一个脚本甚至运行了。 You should be iterating on cart.items not cart. 您应该在cart.items而不是cart上进行迭代。

for(var i = 0; i< cart.items.length; i++){
    if(cart.items[i].vendor == 'example'){
        inArray = true; 
        break;
    }
}

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

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