Filter to remove square brackets and join arrays

Filter to remove square brackets and join arrays

jake_mitchell
Shopify Partner
120 2 61

Hi, 

 

I'm trying to join the values within each products metafield into a new variable and then add this to the order metafield. I think the values from each products metafields are in arrays that need to be combined in order to then pass the combined values into the correpsponding metafield on the order.

 

Example:

 

  • product A has department metafields of "Menswear" and "Womenswear"
  • product B has department metafields of "Books & Media"  and "Homeware"

I combine these into a variable which I think try to put into the order metafield using this code: 

 

{% capture metaDepartment %}
{% for lineItems_item in order.lineItems %}
{% for metafields_item in lineItems_item.product.metafields %}
{% if metafields_item.namespace == "product" and metafields_item.key == "department" %}
{{ metafields_item.value | remove: "[" | remove: "]" | remove: '"' | split: ", " }}
{% endif %}
{% endfor %}
{% endfor %}
{% endcapture%}

{{ metaDepartment }}

 

I get this error though. 

 

 

jake_mitchell_0-1673205169771.png

 

What I'm wondering is whether I can join it all together in a way that will be accepted into the new order metafield, and for this to work no matter how many products I have in the order. 

 

My thinking is that I need to retain it in ["value", "value2", "value3"] format but lose the square brackets except the very first and very last and then join arrays using, to get it into the above format. 

 

Having trouble getting there though. Anyone know? Am I barking up the wrong tree here? 

Thanks

Replies 3 (3)

moek
Shopify Partner
7 0 1

Hello! Not sure your question is still relevant. By the way, did you try to use a filter on your metafield value?  Like instead of removing all of this things, just use:

{{ metafields_item.value | metafield_text}}
 

 Let me know please works it in your case or not

I do love helping people to get the result.
I develop websites, web apps, Shopify stores, and much more.
Email: info@devmoek.com
Website
jake_mitchell
Shopify Partner
120 2 61

Hi @moek,

That's a good suggestion. unfortunately it is one of those filters that isn't available in Flow. I did eventually get to a solution on this though. 

wonena-dev
Shopify Partner
21 0 7

Can you share with me the solution? I am having the same issue

Building a Sustainable Marketplace from Scratch