Trying to access metafields in a theme block with a dynamic variable stored in a snippet

Trying to access metafields in a theme block with a dynamic variable stored in a snippet

Nerlian
Shopify Partner
2 0 0

Hi, this might even be the wrong approach, so if you have a better suggestion, let me know.

 

I'm building an app with theme blocks and I'm using product metafields with a namespace of the $app:my-namespace kind. This works well, since if I do (123456 is the app id, for those not in the know):

{{ product.metafields.app--123456--my-namespace.data.value.field }}

It shows whats in there correctly. But I have a lot of blocks and if I'm gonna have multiple apps (for development and such) I don't want to go to each of them and change it, I'd rather have it all in one place, so I created an "appid.liquid" snippet

{%- assign app_id = "123456" -%}
{%- assign metafield_key = "app--" | append: app_id | append: "--my-namespace" -%}
{{- metafield_key | strip -}}

Then in each block I have this (because I thought the whitespaces where the problem)

{%- capture metafield_key -%}{%- render 'appid' -%}{%- endcapture -%}
{%- assign metafield_key = metafield_key | strip | strip_newlines -%}

{{ product.metafields[metafield_key].data.value.field }}

metafield_key contains the correct values because I've printed it and copy pasted it to access the metadata and it works. 

 

But the metafield information doesnt show, i've tried a 

{{ product.metafields[metafield_key] }}

and it gives me a {}

 

I've also tried 

{% assign metafield_value = product.metafields[metafield_key] | default: '' %}
{{ metafield_value.data.value.field }}

And the other with the capture method and it just doesn't work.

Why doesn't it work? Is this even the best way to do this?

Replies 0 (0)