Using assign to exclude a tag from collection.products. Is it possible?

Hi, I am trying to modify my collection.products for loop to exclude products with a tag labelled as “hide”. Below is the sample from the documentation. Any way to modify this to say "where tags do not equal ‘hide’?

{% assign visible_products = collection.products | where: 'vendor', "Polina's Potent Potions" %}

I’m aware i can use unless inside the loop, but that does not play friendly with ajaxify for endless scrolling. I need those products tagged as “hide” to not exist in the array of products.

You don’t necessarily need to create a new array. The part where for loop starts to display the products in the collection, add an if statement to check if the product does not contain a hide tag and render the product snippet for only those products.

Your if statement would basically look like this

{% if product.tags != ‘hide’%}

Render product snippet

{% endif %}

This will save from creating new array and then looping over it and other hassle.

Hope this helps.

Best

Shadab

This causes the same issue as using {% unless product.tags contains ‘Hide’ %}. It is part of the products array and causes pagination issues with ajaxify. The counter hits 30, even though 25 are displayed, because 5 are in the array and hidden from being printed. My solution requires products with the tag of “Hide” to not be a part of the array at all.

You are right my bad. You can still use the if or unless statement outside of the for loop to create a new array and use that array to loop. Why use this advanced filtering if you have if or unless?

Thank you so much, Can you show me on how to create a new array using unless or if? I can’t seem to find any working examples.

I will give you an example code of how i created an array for available and sold out products and created a new array from to have avialable products first and then sold out products for a client.

{% assign available_products = ‘’ %}
{% assign sold_out_products = ‘’ %}

{% for product in collection.products %}
{% if product.available %}
{% assign available_products = available_products | append: product.handle | append: ‘,’ %}
{% else %}

{% assign sold_out_products = sold_out_products | append: product.handle | append: ‘,’ %}
{% endif %}

{% endfor %}

{% assign sorted_products_handles = available_products | append: sold_out_products %}

{% assign final_array = sorted_products_handles | split: ‘,’ %}

Hope this helps your case

Best
Shadab