仅当所有购物车商品都具有相同的特定标签时才允许 shopify 购物车结帐

Allow shopify cart to checkout only if all cart items have same specific tag

这是我的情况:

我似乎无法弄清楚结帐的编码,以查看是否所有产品都具有匹配的 "location-" 标签。我也想对“-product”标签做同样的事情。

我已经尝试过这段代码,但它只会查看其中一个是否存在,而不是每个是否至少有一个匹配项:

{% for item in cart.items %}
    {% assign different_locations = false %}
    {% if item.product.tags == 'location-atwater' or 'location-nordelec' or 'location-place-ville-marie' %}
    {% assign different_locations = true %}
    {% endif %}
    {% endfor %}

    {% if different_locations == true %}
    [ 'CANNOT COMPLETE ORDER' ]
    {% else %}
    <p>
      <input type="submit" class="action_button add_to_cart" id="checkout" name="checkout" value="{{ 'cart.general.checkout' | t }}" />
    </p>
    {% endif %}

希望堆栈溢出社区可以提供帮助。

您的条件不正确

 {% if item.product.tags == 'location-atwater' or 'location-nordelec' or 'location-place-ville-marie' %}

这是错误的。

应该是

{% if item.product.tags == 'location-atwater' or item.product.tags =='location-nordelec' or item.product.tags == 'location-place-ville-marie' %}

试试这个..

我认为如果同时使用液体和 Javascript,您会更轻松地处理标签。将类似以下内容添加到主题的购物车模板中即可开始:

<script>
    (function (){
    var productTags = {};
    {% for item in cart.items %}
    productTags[{{item.product.id}}] = [{% for t in item.product.tags %}"{{ t }}",{% endfor %}];
    {% endfor %}
    var locations = {}; 
    var days = {};
    //locations and days used as Sets. You can extend this to group items by tag e.g locations[t] (locations[t] || []).concat(id);
    for(var id in productTags){
      var tags = productTags[id];
      tags.forEach(function(t){
        if(t.indexOf('location-') == 0){
          locations[t] = true;
        }else if(/^(monday|tuesday|wednesday|thursday|friday|saturday|sunday)-/.test(t)){
          days[t] = true;
        }
      });
    }
    function toList(set){
        var l = [];
      for(var k in set) l.push(k);
      return l;
    }
    var locationList = toList(locations);
    var daysList = toList(days);
    if(locationList.length > 1){
      alert('too many locations: '+locationList.join(', '));
      jQuery("button[name='checkout']").html('no go');
    }

  })();


</script>  

查找所有位置标签,然后按唯一标签过滤:

{% assign location_tags = '' %}

{% for item in cart.items %}
  {% for tag in item.product.tags %}
      {% if tag contains 'location' %}
        {% capture location_tags %}{{ location_tags }},{{ tag }}{% endcapture %}
      {% endif %}
  {% endfor %}
{% endfor %}

{% assign unique_location_tags = location_tags | remove_first: ',' | split: ',' | uniq %}

{% if unique_location_tags.size > 1 %}
  Disable checkout button...
{% endif %}

或者,您可以选择仅向数组添加唯一的位置标签(这样就不需要 uniq 过滤器):

{% if tag contains 'location' %}
  {% unless location_tags contains tag %}
    {% capture location_tags %}{{ location_tags }},{{ tag }}{% endcapture %}
  {% endunless %}
{% endif %}