Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Re: How can I determine the size of a metafields list?

Solved

How can I determine the size of a metafields list?

henglw0621
Tourist
5 0 1

Hi, I have tried the following approach, but it doesn't work. I wonder how I can get the size of the metafields list.

 

 

{% if product.metafields.products.related.value.size > 0 %}
<p>Some related products here.</p>
{% else %}
<p>It is empty.</p>
{% endif %}

 

 

Where {% product.metafields.products.related.type %} is list.product_reference.

 

 

Accepted Solution (1)

LitExtension
Shopify Partner
4877 1003 1167

This is an accepted solution.

Hi @henglw0621,

Sorry for the confusion, you can use the following code:

{% if product.metafields.products.related.value != blank %}
<p>Some related products here.</p>
{% else %}
<p>It is empty.</p>
{% endif %}

Hope it helps!

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

View solution in original post

Replies 8 (8)

Tinytasks
Explorer
39 3 5

Hi @henglw0621 

 

Can you let us know where you want to display the metafields and the theme which you are using?

henglw0621
Tourist
5 0 1

Hi @Tinytasks, thank you for reply.

 

I am using the following theme and display the metafields on the product page.

https://themes.shopify.com/themes/empire/styles/supply

LitExtension
Shopify Partner
4877 1003 1167

Hi @henglw0621,

You can refer: https://shopify.dev/api/liquid/objects#paginate

Hope it helps!

LitExtension - Shopping Cart Migration Expert
Check out our Shopify migration app to migrate your online store to Shopify
henglw0621
Tourist
5 0 1

Hi @LitExtension, thank you for reply. 

 

I have checked the pagination reference you provided, but I don't know how I can apply it to my code. I would appreciate if you could give me some example code on how to apply pagination with the metafield list.

LitExtension
Shopify Partner
4877 1003 1167

This is an accepted solution.

Hi @henglw0621,

Sorry for the confusion, you can use the following code:

{% if product.metafields.products.related.value != blank %}
<p>Some related products here.</p>
{% else %}
<p>It is empty.</p>
{% endif %}

Hope it helps!

LitExtension - Shopping Cart Migration Expert
Check out our Shopify migration app to migrate your online store to Shopify
henglw0621
Tourist
5 0 1

@LitExtension Thank you so much! You saved my day.

TobiEcom
Shopify Partner
2 0 0

Although this has been solved, here is a simpler solution:

 

Size of Metafield list = {{ product.metafields.products.related.value | size }}

Elena_M
Shopify Partner
3 0 1

{% liquid

assign related_products = product.metafields.products.related | split: ','

echo related_products | size

%}