Skip to content

Instantly share code, notes, and snippets.

@TeamDijon
TeamDijon / array-constructor.liquid
Last active June 19, 2024 02:47
Use-cases where creating arrays in Liquid can prove useful
{% comment %}
In this case, the merchant, for UX testing would like subscription products to be on top of the cart drawer, followed by the one-time purchase items.
Usually, we would have two forloops inside the markup, with their distinctive conditions in order to sort the different products.
Instead, we will prepare all the necessary data beforehand and loop over the more explicit arrays in the markup.
For performance, in place of two for loops over the items, we only have one. In more critical use-cases, several ms are gained.
For readability and maintenability, we have a DRY declaration at defined location which makes it more maintainable and readable.
Note: The arrays are constructed using a for loop, a common pattern that works pretty well !
{% endcomment %}
@TeamDijon
TeamDijon / new-cart-filters.liquid
Last active June 22, 2024 09:27
Testing of the new "cart filters" issued by Shopify
{% # Cart filters testing %}
{% liquid
# A small liquid snippet to compare the two new filters from what was possible before
# See https://shopify.dev/docs/api/liquid/filters/cart-filters for reference
# Quite redundant in my opinion as this is basically sugar but maybe they'll find their audience
assign targetProduct = all_products['product-handle']
assign productId = targetProduct.id
assign targetVariant = targetProduct.selected_or_first_available_variant
assign variantId = targetVariant.id