skip to Main Content

In a page.[].liquid template there are numerous ‘vendor’ products to be listed. Some with and some without a collection association.

How can I exclude a specific vendor in a for loop using ‘unless’ or a ternary?

Neither of the following generate any output within the parent container.

<div style="height: 50px;" class="ptest">

  {% assign collection = product.available %}
  {% for product in collection.all_vendors %}
    {% if product.collection != "acme" %}
     <div>yes</div>
     {{ product.name }}
    {% else %}
      <div>no</div>
      {{ product.name }}
    {% endif %}
  {% endfor %}

  {% for product in collection.all_vendors  %}
    {% unless product.vendor contains "acme" %}
     <div>yes</div>
     {{ product.name }}
    {% else %}
      <div>no</div>
      {{ product.name }}
    {% endunless %}
  {% endfor %}

</div>

2

Answers


  1. Chosen as BEST ANSWER

    After a lot of searching, it looks like the only solution to displaying products that A. are 'all except' a single vendor and B. are not part of any collection, is to use a more simple if-math.

        {% for product in collections.all.products %}
          {% if product.collections.size < 1 && product.vendor != "acme" %}
                <h3>
                  <a href="{{ product.url }}">
                    {{ product.title }}
                  </a>
                </h3>
          {% endif %}
        {% endfor %}
    

    Running through all products to loop those that are not part of a collection, generates the proper result.


  2. List all vendors except “acme”:

    {%- for vendor in collection.all_vendors -%}
      {%- if vendor == "acme" -%}
        {%- continue -%}
      {%- endif -%}
    
      {{ vendor }} is definitely not "acme"<br>
    {%- endfor %}
    

    List collection products except those with vendor “acme”:

    {%- for product in collection.products -%}
      {%- if product.vendor == "acme" -%}
        {%- continue -%}
      {%- endif -%}
    
      {{ product.name }} vendor is {{ product.vendor }}.<br>
    {%- endfor -%}
    

    Sorry, but your code is a mess:

    • {% assign collection = product.available %} – the collection is now boolean, either true or false
    • {% for product in collection.all_vendors %} – as of the above collection doesn’t have all_vendors attribute.
    • {% if product.collection != "acme" %} – as of the above, this code will never be reached, but even if it will – the product object doesn’t have collection attribute.
    • {{ product.name }} – where did you get this code? product doesn’t have name attribute, you have to use title instead.
    • {% for product in collection.all_vendors %} – you go through vendors, why do you call them products?
    • {% unless product.vendor contains "acme" %} – as this is the next line after the above one, the product would be a vendor, not a product object, so you should use it as a string i.e. {% unless product contains "acme" %}
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search