Single Line Text Metafield with list of values can not be joined

Hi everyone, happy Easter!

I’ve realised a strange behaviour when adding a meta field as an single line text with list option.

For example I defined a meta field for a product with name “taste” and values “lemon” and “mint”.

When accessing this metafield value from liquid code, I assume I can join the values with a comma, because if I print the value to console, it is definitely written as an Array of those values.

But when I join the values with the join pipe of liquid, they are still print out like the following:

[“lemon”,“mint”]

like nothing has happened. Could that be a bug?

Hi @stoppel ,

It seems to be a problem with the Liquid code format. The thing is that the payload returned for the “list” metafields if you use the following code pattern:

{{ product.metafields.namespace.key }}

is a JSON array and not a Liquid array:

Which means that you can’t apply the Liquid filters to this array. When dealing with a JSON array in Liquid code, it’s pretty much the same as dealing with a string:

However, if you change the Liquid code format to this:

{{ product.metafields.namespace.key.value }}

then you’ll get a Liquid array which can be joined using a standard filter:

4 Likes

Thank you really much for your help and the explanation!

@MetafieldsGuru Do you maybe have also a tip for me, how I can use the metafield in my section input parameter? When I am using it as data source, it automatically concats the values with and “and” and i can not use them properly in code.

Hi @stoppel ,

Sure, you’re welcome!

As for the dynamic data sources, I don’t think that it’s possible change the way they display the data on the product page. Since you need to have more control over the data, you may want to insert the Liquid code directly in the product template instead. Here’s the link to a step-by-step tutorial on how to do that. Jump to the second part of the tutorial for more details.

1 Like

Hello @MetafieldsGuru your reply was very helpful.

I would also like to ask, if it is possible to translate metafields “single line text with list option” into other languages? if such a possibility?

@MetafieldsGuru how do I make it into a new line, I have tried the code below but that didn’t work

{{ product.metafields.custom.care_insturction.value | join:"n/" }}
 {{ product.metafields.custom.care_insturction.value | join:"br/" }}
 {{ product.metafields.custom.care_insturction.value | join:"/n" }}

I’m using debut them and editing in product-template.liquid