Solved

Display only unique product metafields in email

meganatmetro
Excursionist
28 4 4

Looking for an alternative way of displaying the information below. If the customer purchases multiple classes with the same instructor, (ex. James), I want James' information to display only once. The code I have below works, though ideally, I would like to avoid having to manually input 'James', and rely solely on liquid to determine if the metafield has repeated. There are multiple instructors, and more will be added in the future. I've been playing with code all day, and haven't found a solution. If you have any solutions to offer, I'm all ears! Thank you.

 

{% assign count = 0 %}
    {% for line in line_items %}
      {% if line.product.metafields.custom.trainer contains "James" and count < 1 %}

      {% assign url = line.product.metafields.custom.address_url %}
      {% assign trainer = line.product.metafields.custom.trainer %}
      {% assign bio = line.product.metafields.custom.trainer_bio %}
      {% assign handle = line.product.metafields.custom.instagram_handle %}
      {% assign image = line.product.metafields.custom.trainer_image %}

        {% assign count = count | plus: 1 %}

        <section class="full">
          <div class="half"><img src="{{ image }}"></div>
          <div class="half"><h2>Meet your trainer</h2><h3>{{ trainer }}</h3><p>{{ bio }}</p><p>Check out <a href="https://www.instagram.com/{{ handle }}">@{{ handle }}</a> on Instagram for a preview of his work!</p></div>
        </section>

        {% elsif count == 1 %}
        {% break %}
      {% endif %}
    {% endfor %}

 

Accepted Solution (1)

PaulNewton
Shopify Partner
6274 572 1310

This is an accepted solution.

Hi @meganatmetro What you've go so far is great like you've avoided the count reset trap.

In this sample you might not even need to count:

{%- assign trainers_rendered = '' -%}
{%- for line in line_items -%}
  {% if line.product.metafields.custom.trainer %}
    {% unless trainers_rendered contains line.product.metafields.custom.trainer.value %}
      {%- comment -%}content here{%- endcomment -%}

      {%- assign trainers_rendered = trainers_rendered | append: line.product.metafields.custom.trainer.value | append:"," -%}
      {% assign count = count | plus: 1 %}
    {% endunless %}
  {% endif %}
{%- endfor %}

 💣 Do note how string matching can introduce string ambiguity problems i.e. James will match Jameson, etc. So using name + instragram handle would probably be a better unique identifier.

 

For managing sets of related data, like a trainers info,  you may want to look at metaobjects.

metaobjects also can automatically generate a page with entries.

https://help.shopify.com/en/manual/custom-data/metaobjects 

Liquid usage basics https://shopify.dev/docs/api/liquid/objects/metaobject 

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


View solution in original post

Replies 2 (2)

PaulNewton
Shopify Partner
6274 572 1310

This is an accepted solution.

Hi @meganatmetro What you've go so far is great like you've avoided the count reset trap.

In this sample you might not even need to count:

{%- assign trainers_rendered = '' -%}
{%- for line in line_items -%}
  {% if line.product.metafields.custom.trainer %}
    {% unless trainers_rendered contains line.product.metafields.custom.trainer.value %}
      {%- comment -%}content here{%- endcomment -%}

      {%- assign trainers_rendered = trainers_rendered | append: line.product.metafields.custom.trainer.value | append:"," -%}
      {% assign count = count | plus: 1 %}
    {% endunless %}
  {% endif %}
{%- endfor %}

 💣 Do note how string matching can introduce string ambiguity problems i.e. James will match Jameson, etc. So using name + instragram handle would probably be a better unique identifier.

 

For managing sets of related data, like a trainers info,  you may want to look at metaobjects.

metaobjects also can automatically generate a page with entries.

https://help.shopify.com/en/manual/custom-data/metaobjects 

Liquid usage basics https://shopify.dev/docs/api/liquid/objects/metaobject 

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


meganatmetro
Excursionist
28 4 4

With the exception that I had to remove '.value' from 'line.product.metafields.custom.trainer.value', this works perfectly!!! 🌟 Thank you so much for your input! As far as the ambiguity problems, the metafields do also include the trainers last name, which I just didn't include in the clip, and should hopefully be specific enough that it prevents any issues.

 

I do plan on incorporating metaobjects as soon as I'm granted access, which won't happen until next week. However, this solution still works for other parts of the code that I didn't include in this clip! Thanks again!! 🙂