Skip to content

Instantly share code, notes, and snippets.

@TeamDijon
TeamDijon / color-contrast.liquid
Created June 22, 2024 12:58
A small snippet that returns the most-contrasted color, given a color
{% comment %}
Returns the color code most contrasted with the given color.
Accepts:
color {Hex color} - The background color of the element. Defaults to #ffffff.
black {Hex color} - The first color we calculate the contrast with. Defaults to #000000. (Optional)
white {Hex color} - The second color we calculate the constrast with. Defaults to #ffffff. (Optional)
Usage:
{% render 'color-contrast', color: section.settings.background_color %}
@TeamDijon
TeamDijon / cross-sell-data.liquid
Last active July 10, 2024 08:22
Playing with object construction in Liquid
{% comment %}
- I have a product with a variant metafield containing associated cross-sell product list
- Objective is to show cross-sell products associated with the variant
- Second objective was to use object construction to remove complexity in markup
- Inside the markup, we need the product reference for data population as well as the variant IDs which needs to show the cross sell card (used by Javascript)
{% endcomment %}
{% liquid
# First, we make a duplicate-free list of cross-sell products
assign cross_sell_product_list = ''
@TeamDijon
TeamDijon / file-documentation.liquid
Last active June 21, 2024 08:40
More explanation regarding file organization
{% # Snippet documentation comment %}
{%- comment -%}
Renders the template name based on the template object.
Accepts:
- template {Template object} - Template object
Usage:
{% render 'template-name' %}
{%- endcomment -%}
@TeamDijon
TeamDijon / where-filter.liquid
Last active June 17, 2024 14:09
Use cases for the where filter in Shopify Liquid
{% # 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
%}
/////////////////////////////////////////
@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