Topics covering webhook creation & management, event handling, Pub/Sub, and Eventbridge, in Shopify apps.
In many other languages, it's common to reach specific items by using an index to locate specific items in an array.
In this specific task, I'm trying to access an item in a metafield list.
Given the metafields returns an array - I would expect it to be possible to reach a specific item.
For example:
{{ product.metafields.shopify['color-pattern'].value }} // Returns an array
Which allows for looping:
{% for colorPattern in product.metafields.shopify['color-pattern'].value %}
// colorPattern.color; #HEX_CODE
{% endfor %}
I'm already having a forloop active a few lines above, allowing me to access the specific index with forloop.index0. I wouldn't want to run an unnecessary, nested forloop.
However, this does not work as I would expect
{% assign availableColors = product.metafields.shopify['color-pattern'].value %}
<div
class="color-palette"
style="background-color: {{ availableColors[forloop.index0] }}" // returns NULL
>
...
Nor does any other (what I could spot) way of reaching the value with indexing, either.
Is is possible to access metafields' list items by indexing, or will I have to resort to nested forlooping?
Thanks.