仅当所有购物车商品都具有相同的特定标签时,才允许shopify购物车结帐
问题描述:
这是我的情况:
- 我为产品设置了2个主要集合/标签:日期(例如:星期一产品)和位置(例如:位置-vegas).所有的一天标签都具有相同的"-product"后缀,并且每个位置标签都具有相同的"location-"前缀
- 所有产品都可以贴上一日标签"monday-product"和一个位置标签"location-vegas".
- 如果某人购买了带有"location-vegas"标签的产品和另一个带有"location-la"标签的产品,则结帐将不起作用,因为它们之间没有匹配的"location-"标签.
我似乎无法弄清楚结帐的代码,以查看是否所有产品都具有匹配的"location-"标签.我也想对"-product"标签做同样的事情.
I can't seem to figure out the coding for the checkout to look if all products have matching "location-" tags. I would also like to do the same with the "-product" tags.
我已经尝试过此代码,但是它只会查看其中是否有一个,而不是每个都至少具有一个匹配项:
I've tried this code but it only looks if either is there, not if each has at least one matching:
{% 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 %}
希望堆栈溢出社区可以提供帮助.
Hoping the stack overflow community can help.
答
查找所有位置标签,然后按唯一标签过滤:
Find all location tags and then filter by unique tags:
{% 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
过滤器):
Alternatively, you could choose to only add unique location tags to the array to begin with (and then you don't need the uniq
filter):
{% if tag contains 'location' %}
{% unless location_tags contains tag %}
{% capture location_tags %}{{ location_tags }},{{ tag }}{% endcapture %}
{% endunless %}
{% endif %}