FROM CACHE - en_header

Liquid array .size suddenly not working.

MaxNodland
Excursionist
14 1 5

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 6 (6)
Michal17
Shopify Partner
835 73 158

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 5

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.

LitExtension
Shopify Partner
4428 904 932

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 - #1 Shopping Cart Migration Expert
If our answer is helpful then please accept as solution!
Get your 1-month Shopify free trial from LitExtension. CONTACT US!
Are you looking for a solution to migrate to Shopify? Try our FREE DEMO now!
Michal17
Shopify Partner
835 73 158

Hello @MaxNodland,

You are welcome. Did it work for you?

MaxNodland
Excursionist
14 1 5

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 158

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.