Last active
June 17, 2024 14:09
-
-
Save TeamDijon/bb9fab2e31e5842f95d9f222061adb1c to your computer and use it in GitHub Desktop.
Use cases for the where filter in Shopify Liquid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% # Checking the presence of a block %} | |
{% liquid | |
assign announcement_block_list = section.blocks | where: 'type', 'announcement' | |
if announcement_block_list == empty | |
# Take action to prepare fallback content | |
endif | |
%} | |
///////////////////////////////////////// | |
{% # Limitless metaobject retrieval from handle %} | |
{% liquid | |
assign metaobject_value_list = shop.metaobjects.color.values | |
assign target_handle = 'yellow' | |
assign target_system = metaobject_value_list | map: 'system' | where: 'handle', targer_handle | first | |
if target_system | |
assign target_metaobject = metaobject_value_list | where: 'system', target_system | |
endif | |
# Now, the 'target_metaobject' variable contains the MetaobjectDrop corresponding to the Yellow color | |
# Note that if you have more than 50 entries, the target entry may not be part of 'metaobject_value_list' | |
%} | |
///////////////////////////////////////// | |
{% # Get product variants corresponding to customer group %} | |
{% liquid | |
assign visitor_translation = 'user_group.visitor' | t | |
assign vip_translation = 'user_group.vip' | t | |
assign user_group = customer.metafields.user.group.value | default: visitor_translation | |
if user_group == vip_translation | |
assign product_variant_list = product.variants | where: 'option1', vip_translation | |
else | |
assign product_variant_list = product.variants | where: 'option1', visitor_translation | |
endif | |
# This logic will render correct data for both VIP customers as well as visitors | |
%} | |
///////////////////////////////////////// | |
{% # Sort metaobject entries by creation date %} | |
{% liquid | |
# You need to setup a "date" field inside the metaobject | |
assign metaobject_value_list = shop.metaobjects.color.values | |
assign date_list = metaobject_value_list | map: 'date' | compact | sort_natural | |
for date in date_list | |
assign associatedEntry = metaobject_value_list | where: 'date', date | |
assign sorted_value_list = sorted_value_list | concat: associatedEntry | uniq | |
endfor | |
# 'sorted_value_list' is now ordered according to the date | |
# Note that if you have more than 50 entries, the sorting may be incomplete as some entries are ommited | |
%} | |
///////////////////////////////////////// | |
{% # Recreating the specialized cart filters with existing ones %} | |
{% liquid | |
assign newVariantInCartQuantity = cart | item_count_for_variant: variantId | |
assign oldVariantInCartQuantity = cart.items | where: 'variant_id', variantId | map: 'quantity' | sum | |
assign newProductInCartList = cart | line_items_for: targetProduct | |
assign oldProductInCartList = cart.items | where: 'product_id', productId | |
assign newVariantInCartList = cart | line_items_for: targetVariant | |
assign oldVariantInCartList = cart.items | where: 'variant_id', variantId | |
%} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment