filter collection products by exclude tags

dthien
Shopify Partner
1 0 0

I want to add a tag on product to hide them on store front.
And i got solution for this

 

 

 

{%- for product in collection.products -%}
    {% unless product.tags contains 'mytag' %}
        {{ product.name }}
    {% endunless %}
{%- endfor -%}

 

 

 

But after this, i got problem with paginate, some page have no item or just have few of them.

 

So, is there anyway to assign new variable with collection.product after filter them by tags not contain 'mytag'

 

 

 

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

 

 

 

I found this, but is not help me somuch

Reply 1 (1)

kilark231
Visitor
2 0 0

You can create a new collection with the filtered products. Like first of all filter the products by tags using the code you provided and assign the filtered products to a new variable, such as "filtered_products." So, now you can paginate through the "filtered_products" variable instead of the original collection. This way, you'll ensure that pagination works correctly even after filtering products by tags. Here's how you can implement it:

 

{%- assign filtered_products = collection.products | where_exp: 'product', 'product.tags != "mytag"' -%}

{%- paginate filtered_products by 10 -%}
{% for product in paginate.collection %}
{{ product.name }}
{% endfor %}
{%- endpaginate -%}

 

See, if it works....