Contains not working to find a substring of a product tag

rafael_AR
New Member
5 0 0

Hey guys, good day!

 

I am facing an issue with the contains for a product tag that includes ":" , could this be an issue with the operator or is this documented somewhere?

 

for example if a product has the following tag config:max_quantity_per_order:10 and I check if the product contains a sub string of this tag using `product.tags contains "config:max_quantity_per_order" or "config:max_quantity_per_order:" it doesn't work it returns false.

 

Have any of you guys faced a similar issue?

 

Thanks in advance for any help!

Replies 5 (5)

AwareWare
Excursionist
31 0 3

for    :    the character code is       :

rafael_AR
New Member
5 0 0

I think you are referring to something else. I am talking about liquid code to search for specific tag in a product. I found that if the tag contains ":" it does not work when checking in product.tags, if you do a for tag in product.tags and check there it works. I might create an Issue directly in github to get more info.

tim
Shopify Expert
3274 236 1183

Just did a test and this code:

    {% if product.tags contains "config:test" %}
    <p>Contains "config:test" tag</p>
    {% endif %}

works properly at my test site https://t-test-2.myshopify.com/ 

So it's not a problem with semicolon.

 

However, you're running contains check against product.tags, which is an array and in this case the match must be exact (like it is in my example), you can't match a substring when using contains with arrays.

So you may do it like this:

{% assign tags_string =  tags | join: "|||" %}
{% if tags_string contains "config:max_quantity_per_order:" %}
  {% assign max_qty = tags_string | split: "config:max_quantity_per_order:" | last | times: 1 %}
{% if max_qty > 0 %} <!-- limit max qty to {{ max_qty }}! -->
{% endif %} {% endif %}

 

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
rafael_AR
New Member
5 0 0

Thanks @tim that confirms the weird behavior that I was experiencing. So if it is for a specific tag in a for loop contains correctly works as a substring.

 

If it is for .tags which is an array, it stops working as a substring and works as an exact match. 

 

Very inconsistent if you ask me.

 

Thank you!

almouattaz
Shopify Partner
1 0 0

In fact, if you see a string as an array of characters, it becomes consistent. No?