Loop through metaobject metafields

Loop through metaobject metafields

squadcar
Shopify Partner
5 0 1

How does one loop through metaobject metafields? If I know the specific key for a given field, I can access it directly, but it seems like it shouldn't be difficult to loop through them all with a simple for iteration.

 

Example, this works as it should (metaobject is being accessed through a product metafield):

{% assign my_metaobject =  product.metafields.custom.my_metaobject.value %} 
{{ my_metaobject.a_known_key.value }}

I feel like something like the following should easily be possible, but I can't figure it out:

{% assign my_metaobject =  product.metafields.custom.my_metaobject.value %}

{% for unknown_field in my_metaobject.metafields %} //Loop over every metafield in the metaobject
    {{ unknown_field.value }}
{% endfor %}

Any help would be great! Hard coding each metafield seems needlessly un-dynamic 😕

Replies 3 (3)
squadcar
Shopify Partner
5 0 1

That was exactly what I was looking for. Many many thanks 🙂

franciely
Shopify Partner
5 0 2

Hey! Could you share the solution? I'm trying to do the same with no luck and it seems like @peter_pro3's answer was deleted.

EnoRm
Shopify Partner
1 0 0

Hey! This worked out for me.

{% assign metafield = product.metafields.custom.characteristics.value %}
{% for metaobject in metafield %}
{{ metaobject.title }}
...
...
{% endfor %}