Why did the Liquid array .size function stop working in my customized theme?

MaxNodland
Excursionist
14 1 6

I've customized a theme for a client and it uses metafileds to show additional information on product pages.

I check for the existence of the metafields using:

{% if product.metafields.my_metafield_namespace.size > 0 %}

It's worked great for the past few months, but suddenly .size is returning null. Not even 0, just null.

The metafield object returned is not empty, it has the valid data. It's just that the .size function stopped working.

I searched the forums and found this post from 2019 that's very similar https://community.shopify.com/c/Shopify-Design/Liquid-array-length-is-not-supported-anymore/td-p/617... 

Does anyone have clues as to why this is happening? Does Shopify change these low level functions without warning, or do they fail intermittently? I'd love to have a good explanation for my client who's upset that his theme broke.

Thanks for your help!

Replies 10 (10)

Michal17
Shopify Partner
835 73 175

Hello MaxNodLand,

{% if product.metafields.my_metafield_namespace and product.metafields.my_metafield_namespace.size > 0 %}

Please let me know if you have any further questions.

MaxNodland
Excursionist
14 1 6

Michael, thanks for your response.

I believe metafields.invalid_namespace returns an empty array, not null. So .size should return 0.

If metafields.invalid_namespace returned null I would simply say

{% if product.metafields.my_metafield_namespace %}

 and get rid of the .size call altogether.

The real issue is that I had production code working for months and it suddenly broke without any changes to product metadata or theme code.

Michal17
Shopify Partner
835 73 175

Hello @MaxNodland,

You are welcome. Did it work for you?

MaxNodland
Excursionist
14 1 6

Not exactly - the .size function stopped working on the metafields array, even though the array was not empty.

The problem has seemed to resolved itself, in the same way the thread I linked to in the original post mentioned.

Very strange behavior.

Thanks for posting though.

Michal17
Shopify Partner
835 73 175

Hi @MaxNodland 

Hope you're having a great day!

I could help you more I see your code. Could you share the complete code here?

If you have any further questions, please do reach out either here on the forum, or via private message/email.

Masserra
Shopify Partner
2 0 3

It happened again! I'm going totally crazy! I can see the array has multiple items by console logging it, but when using the filter SIZE or looping through the items it prints ZERO or acts as if it has ZERO items to loop through!

Screenshot 2023-06-05 at 12.15.28.pngScreenshot 2023-06-05 at 12.09.35.pngScreenshot 2023-06-05 at 12.10.26.png

LitExtension
Shopify Partner
4860 1002 1141

Hi @MaxNodland

You can try with this snippet:

{%- assign datas = product.metafields.accentuate.product_related_products | split: '|' | uniq -%}

{% if datas.size > 0 %}

  {% for data in datas %}

    {{ data }}

  {% endfor %}

{% endif %}

Hope this helps.

LitExtension - Shopping Cart Migration Expert
Check out our Shopify migration app to migrate your online store to Shopify

Masserra
Shopify Partner
2 0 3

For anyone getting the same issue here's what worked for me, either:

{% assign list = product.metafields.namespace.key | split: ',' %}

 or:

{% assign list = product.metafields.namespace.key | split: ',' | uniq %}


To get deeper into the subject check the GitHub issue thread.

ZephyrSoft
Shopify Partner
3 0 0

Por alguna extraña razón size deja de funcionar en ocasiones.

 

Lo que hice fue algo manual.

{%- assign count = 0 -%}
{%- for media in metaobject.media.value -%}
  {%- assign mediaCount = mediaCount | plus: 1 -%}
{%- endfor -%}

Count: {{ count }}

 

no_robotocha
Shopify Partner
50 2 8

Hi Max, I realise this is an old post but I think it's still being viewed quite often.

 

Instead of .size - try using .count

 

This worked for me and I thought I'd share in case others are looking in the future.

--
No, Robotocha!