If array contains element in Liquid file.
source link: https://www.itwork.club/2021/11/19/arr-contain-ele-in-liquid/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
If array contains element in Liquid file.
Nov 19, 20218点击
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.
{% 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
{% 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.
原文作者: dgb8901,yinxing
原文链接: https://www.itwork.club/2021/11/19/arr-contain-ele-in-liquid/
版权声明: 转载请注明出处
体验小程序「简易记账」
关注公众号「特想学英语」
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK