Created
March 14, 2019 23:52
-
-
Save stefbowerman/ff34b5172d4a5d88650de5bce5cd4a82 to your computer and use it in GitHub Desktop.
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
{%- comment %} | |
Product Section Display Block ID | |
------------------------------------------------------------------------------ | |
Usage: | |
{% include 'product-section-display-block-id.liquid', | |
product: product, | |
blocks: section.blocks | |
%} | |
In the template, do something like the following. Be sure to always coerce to a number when doing comparisons since it's not always clear if you're working with strings or numbers in liquid | |
{% capture display_block_id %} | |
{% include 'product-section-display-block-id', product: product, blocks: section.blocks %} | |
{% endcapture %} | |
{% assign display_block_id = display_block_id | plus: 0 %} | |
{% endcomment -%} | |
{% if product and blocks.size > 0 %} | |
{% assign product_block_id = 0 %} | |
{% assign tag_block_id = 0 %} | |
{% assign collection_block_id = 0 %} | |
{% assign default_block_id = 0 %} | |
{% assign product_collections_handles = product.collections | map: 'handle' | join: ' $ ' %} | |
{% for block in blocks %} | |
{% assign block_id = block.id | plus: 0 %} {% comment %} Coerce to a number {% endcomment %} | |
{% assign display_product = all_products[block.settings.product] %} | |
{% assign display_collection = collections[block.settings.collection] %} | |
{% comment %} The `if _block_ == 0` checks are to ensure that we pull the first matching block {% endcomment %} | |
{% if block.settings.product != blank %} | |
{% if product.handle == block.settings.product and product_block_id == 0 %} | |
{% assign product_block_id = block_id %} | |
{% endif %} | |
{% elsif block.settings.tag != blank %} | |
{% if product.tags contains block.settings.tag and tag_block_id == 0 %} | |
{% assign tag_block_id = block_id %} | |
{% endif %} | |
{% elsif block.settings.collection != blank %} | |
{% if product_collections_handles contains block.settings.collection %} | |
{% assign collection_block_id = block_id %} | |
{% endif %} | |
{% else %} | |
{% if default_block_id == 0 %} | |
{% assign default_block_id = block_id %} | |
{% endif %} | |
{% endif %} | |
{% endfor %} | |
{% comment %} | |
- Output the first block id that matches our logic (decreasing specificity) | |
- This logic sucks, but we can't really use a case/when statement here and there's no way to return early from an if/elsif statement in liquid | |
- We're only trying to output one block id | |
{% endcomment %} | |
{% capture display_block_id %} | |
{% if product_block_id > 0 %} | |
{{ product_block_id }} | |
{% else %} | |
{% if tag_block_id > 0 %} | |
{{ tag_block_id }} | |
{% else %} | |
{% if collection_block_id > 0 %} | |
{{ collection_block_id }} | |
{% else %} | |
{% if default_block_id > 0 %} | |
{{ default_block_id }} | |
{% endif %} | |
{% endif %} | |
{% endif %} | |
{% endif %} | |
{% endcapture %} | |
{{ display_block_id }} | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment