Ensuring equal Text-List and Image-List Metafields
EDIT: SOLVED!
Hello!
I have run into an issue while working with Shopify Metafields that I think could benefit many people if there is an easily findable solution. I have Googled to no avail and I think that in the future others will be doing the same. Let’s try to get a solution out there!
For Context:
I am building a section to be added to a product template which will allow our store to add extended content. The part that I am working on right now is like a carousel that has both an image and text description tied along with it.
The Problem:
Now, I know very well how to use for loops to get through liquid arrays - which is NOT what I’m asking. Instead, I am looking for a solution that allows me to check the number of images and descriptions are equal! This has given me a ton of issues and I’m not quite sure how to implement a simple condition like this.
Let us say there are 2 metafields we are using. The first is a file list (images only) called “feature_gallery_images”. The second is a single-line-text list called “feature_gallery_descriptions”. In the product I am testing this on, it has three images, and three descriptions already added to its metafields.
The Current Code:
My solution so far has not been working and I have gotten to the point where I made the simplest Liquid possible to try and see what’s going on…
Liquid File:
Image.size: {{ product.metafields.custom.feature_gallery_images.size }}
Image.value.size: {{ product.metafields.custom.feature_gallery_images.value.size }}
Description.size: {{ product.metafields.custom.feature_gallery_descriptions.size }}
Description.value.size:{{ product.metafields.custom.feature_gallery_descriptions.value.size }}
HTML Output:
Image.size:
Image.value.size:
Description.size:
Description.value.size:3
Obviously, for our descriptions, “.value.size” is exactly what we a re looking for. However, this solution is not the same for our file list. If both the single-line-text list and file-list are stored as JSON arrays, then why is it not the same solution for finding the size? Does the specification of the file list being images make it different?
If you have a solution to this problem, I’d love to hear it! Otherwise, I’m also including some of my own past solutions. I’m not a huge fan of them, but when I need a quick implementation - they work.
Current Alternate Solutions:
Now despite my extreme dislike for these following solutions, they DO work. I would love for somebody the have a more elegant solution, but in case not, I wanted to share what I’ve come up with in the past.
First, my least favorite solution is to just not check! In my situation, I am the one filling out metafields about 50% of the time. I also CAN go and check that they are being done correctly, but I’d rather the code do that for me.
My second solution solves this by switching the image list metafield into a single line text list. While this does allow me to check that there are the correct number of images, it also prevents me from using ‘image_url’, ‘image_tag’, uploading images directly, and makes for a relatively tedious implementation/upload process.