Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
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!
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.
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.
Hello @MaxNodland,
You are welcome. Did it work for you?
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.
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.
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!
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.
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.
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 }}
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.
Eyyy this worked! Thank youuuu!
We want to take a moment to celebrate the incredible ways you all engage with the Shopi...
By JasonH Oct 15, 2024Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024