skip to Main Content

I am looping through all collections, and creating a preview item with each collection title, image and link. But I have 15 collections I would like to exclude.

Currently I am using ‘contains’ to exclude the 15 I don’t want, but am wondering if theres a cleaner way to write this since its a really long if condition.

Thanks in advance!

Example below:

{% for collection in collections %}
    {% if collection.title contains 'collection-1' or collection.title 
    contains 'collection-2' or collection.title contains 'collection-3' 
    or collection.title contains 'collection-4' or collection.title 
    contains 'collection-5' %}

    {% else %}

        // build item here

    {% endif %}
{% endfor %}

2

Answers


  1. I would create an array of exclusions and check to see if my exclusion array contains the collection in question. (And rather than the title, I would use the collection handle as the handle is guaranteed to only be ‘clean’ names and guaranteed to be unique)

    Example:

    {% assign collection_exclusion_array = 'collection-1, collection-2, collection-3, collection-4, collection-5' | remove: ' ' | split: ',' %}
    {% for collection in collections %}
      {% if collection_exclusion_array contains collection.handle %}
        {% continue %}
      {% endif %}
      {% comment %} Build items here {% endcomment %}
    {% endfor %}
    

    How it works:

    • We cannot directly create arrays in Liquid – we can only make one by taking a string and using the split filter to create our array.
    • By using handles, we guarantee that our list values only contains letters, numbers and hyphens – there’s no chance that our delimiter (in this case, the comma) can accidentally show up as part of the value.
    • We don’t want spaces to be part of the array values, so we remove them before we use the split filter. We could instead just not put spaces between each value, but in my brain that reads like a terrible abuse of grammar. Either omitting spaces the first time or removing them after creating your string will work.
    • Now that we have our array of exclusions, when we loop through collections we can check to see if the current collection’s handle shows up in the list.
    • If found, skip to the next collection using the continue statement – this saves a layer of indentation since we don’t have to have an empty if followed by an else that contains everything that we want to do.

    And there you go! Hope it helps 🙂

    NB: For more information on handles in Shopify, see https://help.shopify.com/en/themes/liquid/basics/handle

    Login or Signup to reply.
  2. An alternate method to achieve your exclusions:

    If you give your collections some sort of flag that indicates that they shouldn’t show up in your collection loop, you can manage each collection directly, rather than maintaining a separate list.

    If we look at the collection page in your admin, though, we don’t get a lot that’s helpful: all we see are things like title, description, etc. Not even a place to give the collection a specific tag!

    Fortunately, collections are able to have metafields – Shopify just has that feature hidden from normal users. Metafields allow you to create additional information for objects in your store (products, collections, pages, etc.), which you can then reference through Liquid.

    You can read more about Shopify’s use of metafields here: https://www.shopify.com/partners/blog/110057030-using-metafields-in-your-shopify-theme

    My previous favourite plugin for accessing metafields was ShopifyFD, a browser extension that would let you view and edit that metadata right on your collection page, but unfortunately Shopify’s recent changes to the admin have broken that plugin. The author is working on a new version, but it’s not ready at the time of writing: https://freakdesign.com.au/blogs/news/shopifyfd-and-the-current-case-of-the-broken-tool

    (Note: I haven’t tried any of the other metafield-editing tools listed in the above linked article – when ShopifyFD started having trouble, I started doing my metafield editing using the admin API and creating/posting the requests myself: https://help.shopify.com/en/api/reference/metafield)

    Once you have a way to easily set metafields (which, surprisingly, seems to be the hard part right now), your for-loop logic is extremely simple. Let’s assume that the metafield you create for this purpose has the namespace ‘preview’ and the key ‘exclude’:

    {% for collection in collections %}
      {% if collection.metafields.preview.exclude %}
        {% continue %}
      {% endif %}
      {% comment %}  Do stuff! {% endcomment %}
    {% endfor %}
    

    This will now skip any collection that has any value set in your custom field, so if you change your mind about any current or future collection all that needs to change is the one metafield on the collection itself.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search