Liquid's concat filter not working on arrays from shopify metafields?

191 Views Asked by At

Liquid's concat filter works like a charm using it on self-created arrays or arrays from section settings. However, I cannot get it to work when using it on arrays from metafields of a shopify product.

I have two metafields on my products: One holding a list of products and one holding a list of product variants. The types of the metafields are list.product_reference and list.variant_reference. I want to merge both lists:

{%- assign products = product.metafields.custom.fcv_products.value -%}
{%- assign variants = product.metafields.custom.fcv_variants.value -%}
{%- assign items = products | concat: variants -%}

However, items will be null. When outputting this expression in the console like this:

<script>console.log({{ products | concat: variants | json }})</script>

I get the following error: Liquid error (sections/...): concat filter requires an array argument

Both arrays contain elements. The following logs all work as expected:

{%- for variant in variants -%}
  <script>console.log({{ variant | json }}); // Prints variants from the list </script>
{%- endfor -%}
<script>console.log({{ products | json }}); // Prints an array with products </script>
<script>console.log({{ variants | json }}); // Prints an array with variants </script>
<script>console.log({{ items | json }}); // Prints null </script>

Prior to this solution using the metafields, I used something similar to the following, which worked perfectly fine:

{%- assign products = section.settings.products -%}
{%- assign variants_nested = products | map: "variants" -%}
{%- assign variants = "" | split: "" -%}
{%- for variants_list in variants_nested -%}
  {%- assign variants = variants | concat: variants_list %}
{%- endfor -%}
{%- assign items = products | concat: variants -%}

Because of changed requirements we have to switch to the metafields solution.

Any ideas?

0

There are 0 best solutions below