Help with filtering function

Hi there,

First of all I must say that Im not a coder. So I have no basic skills what so ever.

However, in my online store each product is tagged with a label.
For example Mark_Gant, Mark_Oakley, Mark_Diesel and so on.

I have some problems to get the filtering function to work as I want on the production page.

I found a code-snippet that seems to work halfway. But I need it to show every product that is tagged “Mark_”
regardless of which brand comes after.

This shows all products that is tagged Mark_Gant for example:

{% assign show_tags = “Mark_Gant” | split: “,” %}
{% for tag in collection.all_tags %}
{% unless show_tags contains tag %}
{% continue %}
{% endunless %}

But I want every brand to appear in the “filter by”-list.
It should be something like this?

{% assign show_tags = “Mark_*” | split: “,” %}
{% for tag in collection.all_tags %}
{% unless show_tags contains tag %}
{% continue %}
{% endunless %}

But of course it doesn’t work. :slight_smile:

Of course I can define which tags to filter:

{% assign show_tags = “Mark_Oakley, Mark_Diesel, Mark_Rayban” | split: “,” %}

but it will be lots of tags in that code (100+).

How can I define to show every product that is tagged Mark_*?

Hope you get it. Once again. Im a total rookie with this.

Thanks in advance.

This looks like you are using Shopify Solved: Filter collection with SPECIFIC tags - Shopify Community ?

Then the language used within {% … %} is Liquid?

If so, I think you might be able to replace:

{% unless show_tags contains tag %}

with a check for if “Mark_” is in the tag string:

{% unless tag contains "Mark_" %}

This should really check if tag starts with “Mark_”, but I could only see contains.
See the operators listed on: Operators – Liquid template language

4 Likes

Yes, it’s Shopify and Liquid.

Your code worked great! Many, many thanks! I have been struggling with this for a long time.

Now I have to find a way to “hide” Mark_ in the filter so it looks cleaner :wink:

Namnlös

1 Like

I also have the same problem. Thanks for helping, Guys!

thanks op, you solved my problem too. :smiley: :smiley: