这是我的情况:
我似乎无法弄清楚结帐的编码,看看是否所有产品都有匹配的“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 %}
希望堆栈溢出社区可以提供帮助。
答案 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>