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

时间:2016-01-10 00:03:21

标签: shopify cart

这是我的情况:

  • 我的产品设置了2个主要的集合/标签:day(例如:monday-product)和location(例如:location-vegas)。全天标签具有相同的“-product”后缀,每个位置标签具有相同的“location-”前缀
  • 所有产品都可以标记为“星期一产品”的一天标签和“location-vegas”的一个位置标签。
  • 如果某人购买带有“location-vegas”标签的产品和带有“location-la”标签的其他产品,则结帐不起作用,因为它们之间没有匹配的“location-”标签。

我似乎无法弄清楚结帐的编码,看看是否所有产品都有匹配的“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 %}

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

3 个答案:

答案 0 :(得分:2)

找到所有位置标记,然后按唯一标记过滤:

{% 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 %}

答案 1 :(得分:0)

您的情况不正确

 {% 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' %}

尝试这个..

答案 2 :(得分:0)

如果您同时使用液体和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>