Ad

An Equivalent To Is_array() In Liquid?

- 1 answer

Is there a way to test whether something is an array or not in Liquid? Or if an array is empty? Or if a variable is defined but empty?

I'm trying to write something for Shopify that will only show a particular metafield if the product doesn't have any variants, but there doesn't seem to be any way of testing if product.variants has any content in Liquid.

Ad

Answer

Turns out you can use .size to check the length of a string or the size of an array. For example, if product.variants.size > 1.

Read about Array Filters in Liquid.

Ad
source: stackoverflow.com
Ad