Call a specific metafield from a variable

I’m trying to call specific metafields based on my product tags. Is it possible to call a metafield from a variable like below?

{% assign my_variable = 'bar' %}

call something like this

{{ shop.metafields.foo.my_variable.value }}

to get this

{{ shop.metafields.foo.bar.value }}

?

I have tried all these below with no luck

{{ shop.metafields.foo.my_variable.value }}
{{ shop.metafield.foo.[my_variable].value }}
{{ shop.metafield.foo.['my_variable'].value }}

Or even tried to put the entire metafield address into a variable but didn’t work.

my workaround currently is using pages and blogs to store the information but I’m trying to see if this is possible at all.

thank you

I think your issue is the dot "." before the dynamic access "[my_variable]"

Also metafields should be plural not singular, although that might have just been pseudocode.

The following liquid code should work:

{{ shop.metafields.foo[my_variable].value }}

It worked, thank you very much. I had an extra “.” in there.

Hi Danloomer,

I have a similar issue. I created a few metafields and made it available to orders. I am trying to call them for display on my order page through Printer Pro app.

I thought I would just need to do the following:

{{ order.metafields.custom.purchase_order_number }}

But nothing is displayed. Any suggestions?