Check if specific value exists in multiple value Metafield

Hello,

We are attempting to create code to detect if specific values exist among values in a multiple value Metafield.

For example, Metafield “Color” has 4 color options. Some items may have more than one color. We would like to create an “If” statement based on what colors a product is assigned.

We are doing this to load in a custom icon if the product has specific color metafields. For example, if the item has the Red Metafield Value, we load the Red icon. For White, the White icon. For Red & White, both Red and White icons.

Here is the current code that is not working:

{% for value in product.metafields.custom.color_value.value %}

{% if product.metafields.custom.color_value.value == “White” %}

{% break %}

{% endif %}

{% endfor %}

I assume it is not working because if the item is tagged as being Red AND White, the value is not equal to White, it is equal to Red & White both.

So we would like the code to check if the value exists at all anywhere in the metafield tags, since the order might change. If White was always the first value, we could check if value[0] == “White”, but since it may be 2nd or 3rd, we need a way to reference the full range of values in the metafield.

Any help is appreciated!

Try using

{% if product.metafields.custom.color_value.value contains ‘white’ %}

// code

{% endif %}