If array contains element in Liquid file.

For example, we want to know whether the cart contains a specific product.

First, get a product Array by split in Cart.liquid

Iterate the cart items, get variant ID in each loop. Check if the variant is the one we want.

1
2
3
4
5
6
7
8
9
10
11
{% assign gotIt = false %}
{% assign variantIds = "111, 222, 333" | split:", " %}

{% for item in cart.items %}
{% assign itemVariantId = item.variant.id | downcase %}
{% for id in variantIds %}
{% if id == itemVariantId %}
{% assign gotIt = true %}
{% endif %}
{% endfor %}
{% endfor %}

Or, you want to know if there’s a specific item in Checkout page.

The process is same with above codes.

Notice that you can get the variant_id from Checkout API, it’s in line_item.variant_id

1
2
3
4
5
6
7
8
9
10
11
12
{% assign gotIt = false %}

{% assign variantIds = "111, 222, 333" | split:", " %}

{% for line_item in checkout.line_items %}
{% assign itemVariantId = line_item.variant_id | downcase %}
{% for id in variantIds %}
{% if id == itemVariantId %}
{% assign gotIt = true %}
{% endif %}
{% endfor %}
{% endfor %}

And, you may also do the check by arr contains str grammer.

Liquid Document
Shopify Objects

原文作者: dgb8901,yinxing

原文链接: https://www.itwork.club/2021/11/19/arr-contain-ele-in-liquid/

版权声明: 转载请注明出处

为您推荐

体验小程序「简易记账」

关注公众号「特想学英语」

Harbor离线安装