Skip to content

Instantly share code, notes, and snippets.

@driespieters
Created December 20, 2022 11:16
Show Gist options
  • Save driespieters/55daea4a4f1cbb3402b662ad86402549 to your computer and use it in GitHub Desktop.
Save driespieters/55daea4a4f1cbb3402b662ad86402549 to your computer and use it in GitHub Desktop.
Shopify - Conditionally show/hide section based on country
{%- liquid
assign geo_block = false
assign country_match = false
assign markets_isocodes = section.settings.markets_isocodes | split: ','
if markets_isocodes contains localization.country.iso_code
assign country_match = true
endif
if section.settings.markets == "hide" and country_match == true and request.design_mode == false
assign geo_block = true
elsif section.settings.markets == "show" and country_match == false and request.design_mode == false
assign geo_block = true
endif
-%}
{%- if request.design_mode == true and section.settings.markets != "all" -%}
{%- if section.settings.markets == "hide" -%}
<div class="bg-yellow-100 border border-yellow-300 rounded p-2 mx-4 semibold">Section below will not be shown in countries: {{ section.settings.markets_isocodes }}</div>
{%- else -%}
<div class="bg-yellow-100 border border-yellow-300 rounded p-2 mx-4 semibold">Section below will only be shown in countries: {{ section.settings.markets_isocodes }}</div>
{%- endif -%}
{%- endif -%}
{%- unless geo_block -%}
// Section content
{%- endunless -%}
{% schema %}
{
"name": "Section name",
"settings": [
{
"type": "header",
"content": "GEO settings"
},
{
"type": "select",
"id": "markets",
"label": "Show/hide in countries",
"default": "all",
"options": [
{
"value": "all",
"label": "Show in all countries"
},
{
"value": "hide",
"label": "Hide in countries:"
},
{
"value": "show",
"label": "Only show in countries:"
}
]
},
{
"type": "text",
"id": "markets_isocodes",
"label": "Countries",
"info": "⚠️ Important: Use 2-country letter codes [Learn more](https://shopify.dev/api/storefront/2022-10/enums/countrycode). Use comma's. For example: BE,FR,NL,DE"
}
]
}
{% endschema %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment