skip to Main Content

I’m still very new to Shopify so my knowledge of it is very limited.

I’ve created a page where I want to show certain products with the tag "export". These products should never come up on the collection page (siteurl/collection/all).

I’ve tried excluding the products with the tag from the collection template but that only leaves gaps on the pages its excluding.

enter image description here

This is my current code:

{% for product in collection.products %}
    {% if product.tags contains 'export' %}{% continue %}{% endif %}
       My code for displaying the product
{% endfor %}

I’ve also tried:

{% for product in collection.products %}
    {% unless product.tags contains 'export' %}
       My code for displaying the product
    {% endunless %}
{% endfor %}

My thinking is that I’ll need to set the conditions before I call my products so my pagination doesn’t break. Is there a way to do this in Shopify?

2

Answers


  1. What you need to know is that Liquid performs the initial action (for product in collection.products) before then performing the next one.

    So the forloop grabs all of the products, and then your if statement removes those products. Unfortunately, the pagination has already performed the product split during the forloop, so when you remove the product, it removes it from the paginated result.

    There is, annoyingly, no way around this.
    The only realistic solution is to create your own all collection and then use smart filters to add everything except products tagged with xyz.

    Login or Signup to reply.
  2. This code should work for you, it is rudimentary and includes some code that could be excluded but will help you understand how it works

    CODE FOR YOUR COLLECTION TEMPLATE:

    {% for product in collection.products %}
        <!-- Set the default value of "product_show" to "true" -->
        {% assign product_show = true %}
    
        <!-- We iterate all the tags inside the product checking if one of them EXACTLY matches with "Export" -->
        {% for tag in product.tags %}
    
            {% if tag == "export" %}
                <!-- If we find an "export" tag then we set product_show to "false" -->
                {% assign product_show = false %}
    
                <!-- When we find the label we exit the "FOR" loop -->
                {% break %}
            {% endif %}
        {% endfor %}
    
        <!-- If the product has "product_show" then it will not be shown and will continue with the next product -->
        {% if product_show == "true" %}
            CODIGO DEL PRODUCTO
        {% endif %}
    {% endfor %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search