简体   繁体   中英

Assigning conditional global liquid variables in Shopify

I'm using trying to display shopify content based on the local domain it's using. To do this I've got the below which works fine:

{% if request.host contains 'co.nz' %}
  {% assign site_currency = "NZ" %}
{% endif %}

The problem is that variables seem to only work within the one snippet/template/layout file prior to being compiled, and what Shopify classes as a global variable is in fact just a value from the theme settings and therefore can't be conditional.

I've tried '{% assign scope = 'page' %}' which apparently works after compilation but I can't get it to work and there's very little documentation.

Does anyone know how I can declare this variable just once and then reference it throughout my theme?

Thanks!

Shopify global variables are only the objects specified here - https://shopify.dev/docs/themes/liquid/reference/objects

Only way to acheieve a global reference functionality without referencing through snippets is by storing it in the cart.attributes via JS, but that requires a page refresh to take effect.

Create a snippet or just paste the code above {{ content_for_layout }} in theme.liquid

{% comment %} /snippets/global-vars.liquid {% endcomment %}

{% if request.host contains 'co.nz' %}
  {% assign site_currency = "NZ" %}
{% endif %}

If you have few different currencies I suggest that you look into using case/when , here are the docs.

{% comment %} /layout/theme.liquid {% endcomment %}

...

{% include 'global-vars' %}
{% comment %} this should print the currency {% endcomment %}
{{ site_currency }} 
{{ content_for_layout }}

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM