How can I use a metafield in a conditional for order confirmation notifications?

OK, I need a more eyes on this…

I’m trying to add to my order confirmation notification a conditional that will show text only when any product in the order has a particular value for a particular metafield. I have the following:

{% for line in subtotal_line_items %}
{% if line.product.metafields.custom.one_of_a_kind contains "stock" %}
<p>MESSAGE</p>
{% break %}
{% endif %}
{% endfor %}

Now, if I replace the conditional with the actual metafield instead, like:

{% for line in subtotal_line_items %}
{{ line.product.metafields.custom.one_of_a_kind }}
{% endfor %}

I do see the value of that metafield, so I know I’m using the correct metafield object, and it clearly contains the word “stock” in it. That also suggests to me that the for tag is not the issue. Obviously there is something about metafields and how the data is stored that I don’t understand.

The field is a Single Line Text type limited to preset choices. What am I missing?

It appears that the issue lies in the condition you are using to check the metafield value. The contains operator is used for checking if a string contains another string, but it may not work as expected when used with a metafield value.

Instead, you can convert the metafield value to a string and then use the contains operator to check if it contains the desired text. Here’s an example of how you can modify your code to achieve this:

{% for line in subtotal_line_items %}
  {% assign metafieldValue = line.product.metafields.custom.one_of_a_kind | stringify %}
  {% if metafieldValue contains "stock" %}
    

MESSAGE

    {% break %}
  {% endif %}
{% endfor %}

In the modified code, the | stringify filter is used to convert the metafield value to a string. This allows you to use the contains operator to check if it contains the word “stock”. If the condition is met, the desired message will be displayed.

Make sure to replace the "stock" text with the actual value you want to check for in the metafield. Additionally, ensure that the metafield key (custom.one_of_a_kind) is correct and matches the metafield you are trying to access.

Thanks for your post @NomtechSolution .

I tried your code. Unfortunately, the result was the same. I also tried the | metafield_text filter, which I found in an online reference. In both cases, the assignment of the filtered metafield appeared to work, as I had it display metafieldValue, but the conditional still does not work. You were correct: It was the word “stock” that I was looking for, as that word appears in only that particular option.

I tried dropping the contains operator and replaced it with an == and pasted the entire text field between the quotes. Still no.

Next I tried contains "e" out of curiosity. Nope.

Then, just before I hit reply on this message, I decided to try the | escape filter. I didn’t expect it to do anything—I mean, what about that string needs to be escaped?—but that worked. I double-checked by using a string that wasn’t in the metafield in the conditional, and, as expected, the conditional returned false.

So I have no idea why that resolved the issue, but that was the solution in this case.