How to do something like "for tag in product.tags or line_item.product.tags"

Windsinger
Tourist
19 0 1

I have two snippet files that are pulled into two different pages.

It works in one page if loop states: {% for tag in product.tags %}...

It works in the other file if loop states: : {% for tag in line_item.product.tags %}

Rather than duplicating the snippet files (see excerpts below) and having to maintain separate files, is there a way the files will work in both instances? Something like {% for tag in product.tags or line_item.product.tags %}

{% for tag in product.tags %}
{% if tag contains 'Mix Musically' %}
{% include 'tag-sonic-mixes-musically' %}
{% break %}
{% endif %}
{% endfor %}

... the above then pulls in 

 

<ul>
{% for tag in product.tags %}
{% if tag contains 'Musically Chakra' %}

<li>Chakra Tuning</li>
{% endif %}
{% if tag contains 'Dorian' %}
<li>Inspirational - Ecclesiastical / Medieval Dorian Mode</li>
{% endif %}...

etc. etc.

Thanks for any assistance. I know just enough to get into trouble.

0 Likes

Hi Windsinger,

It's definitely possible — have you looked into the Template Object?

You could do something like...

{% if template.name == 'product' %}
    //Do this
{% else %}
    //Do this
{% endif %}

 

Let me know if that helps!

Thanks,
Kyle

Let's get Shopify figured out together. Get live USA-based customer service for your current or upcoming web project.
Email: hello@freshnarrative.io | tel: (443) 345-8162
Baltimore, Maryland, United States
We offer free and paid Shopify support.
Monday - Friday | 9 AM — 5 PM EST
Windsinger
Tourist
19 0 1

Thanks Kyle,

I didn't even consider that.

Just checking though; wouldn’t that still require that I repeat the same if statements in two separate lists? See example 2 below.

Because the following errors out with the for tag being left open before the list starts.

{% if template.name == 'product' %}

{% for tag in product.tags %}

{% else %}

{% for tag in line_item.product.tags %}

{% endif %}

{% if tag contains 'abc' %}

<li>abc</li>…

{% endif %}

{% endfor %}

 

Example 2:

{% if template.name == 'product' %}

{% for tag in product.tags %}

{% if tag contains 'abc' %}
<li>abc</li>
{% endif %}
{% if tag contains 'def' %}

<li>def</li>… etc. etc.

// repeat same list only change product.tags to line_item.product.tags //

{% else %}

{% for tag in line_item.product.tags %}
{% if tag contains 'abc' %}
<li>abc</li>
{% endif %}
{% if tag contains 'def' %}

<li>def</li>… etc. etc.

Just trying to consolidate the two identical lists into one list. It's not essential but seems like it is the elegant, easily maintained solution.

Thank you again.

0 Likes
Windsinger
Tourist
19 0 1

I was also hoping something like this might work: {% for tag in {% if template.name == 'product' %}product.tags{% else %}line_item.product.tags{% endif %} %}.

Needless to say, it didn't.

I was also hoping assigning a variable at the outset would work, but apparently not. 

{% if template.name == 'product' %}
{% assign sonic_mix = 'product.tags' %}
{% else %}
{% assign sonic_mix = 'line_item.product.tags' %}
{% endif %}
<ul>
{% for tag in {{ sonic_mix }} %}
{% if tag contains 'Theme Chakra' %}
<li>Chakra Theme</li>
    {% endif %}
{% if tag contains 'Theme Celebration' %} etc. etc.
0 Likes

If you'd like I can take a look at your liquid code and provide a full solution.

PM if you're interested!

Thanks,

Kyle

Let's get Shopify figured out together. Get live USA-based customer service for your current or upcoming web project.
Email: hello@freshnarrative.io | tel: (443) 345-8162
Baltimore, Maryland, United States
We offer free and paid Shopify support.
Monday - Friday | 9 AM — 5 PM EST
0 Likes

You can pass values to snippets. Should be pretty simple. Just pass the product as an argument when you're calling the snippet, example below.

 

{% render 'tag-list', product: product %}

 

 

Then just have your conditional logic in the snippet. 

Just helping folks learn Shopify theme development - universitheme.com
Windsinger
Tourist
19 0 1

Thank you . I'll take a look at this solution.

0 Likes
tim
Shopify Expert
2926 143 1021

This should work, provided you remove the quotes from 'product.tags'  and  'line_item.product.tags' and double curly braces around  sonic_mix so it looks like:

{% if template.name == 'product' %}
    {% assign sonic_mix = product.tags %}
{% else %}
    {% assign sonic_mix = line_item.product.tags %}
{% endif %}
{% for tag in sonic_mix  %}
    ...
 
Want to hire me to tweak a theme? Mail me at tairli@yahoo.com!
My post solved your problem? Like it!
PaulNewton
Shopify Partner
2601 136 475

@Windsinger note that @universitheme solution is meant to be used in  a cart.line_items forloop (or line_items in notification templates)

If your checking against several preknown tags and want to get rid of some forloops, then when assigning tags to a variable considering joining tags to a string.

Then use string filters based on your situation to minimize common issues, either in text-comparison or possible bad data entry either in the admin or in the code.

Here I set the hardcoded tags being checked to their own variables for easier updating , use a downcase filter(to avoid character case issues), and strip spaces for tags with multiple words.

{% assign tag1 = "tag1" | downcase | strip:" " %}
{% assign tag2 = "tag2" | downcase | strip:" " %}

{% assign current_tags_filtered = product.tags | join:"," | downcase | strip:" " %}
{% if current_tags_filtered contains tag1 %}
 {% elsif current_tags_filtered  contains tag2 %}
{% endif %}

for LOTs of hardcoded tags you will want to just put them all in a comma separated string split it to an array do a double loop 

Advanced - For performance if there are lots of tags on the object and there are extra forloops use the {% break %} tag once the code has satisficed all possible conditions:

{% assign match_found = false %}
 {% for tag in current_tags %}
  {% if match_found and match_2_found = true %} {% break %} {% endif %}
  {% if true %}
   {% assign match_found = true %}
    .....
  {% elsif true %}
    {% assign match_2_found = true %}
     ......
    {% else %}
 {% endif %}
{% endfor %}

 

Problem Solved? ✔️Accept and ? Like the solution so you can help others.
Buy me a coffee ☕ paypal.me/paulnewton or donate to eff.org
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com
0 Likes