1
votes

Here’s my situation:

  • I have products set up with 2 major collections/tags: day(example: monday-product) and location (example: location-vegas). All day tags have the same "-product" suffix and each location tag has the same "location-" prefix
  • All products can be tagged with one day tag "monday-product" and one location tag “location-vegas”.
  • If a person buys a product with the tags “location-vegas” and another product with the tag “location-la”, the checkout doesn’t work because it doesn’t have matching "location-“ tags between them.

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.

3

3 Answers

2
votes

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

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 %}
0
votes

Your condition is incorrect

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

This is wrong.

It should be

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

try this one..

0
votes

I think you'd have an easier time processing the tags if you used both liquid and Javascript. Adding something like the following to your theme's cart template will get you started:

<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>