skip to Main Content

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!

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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 }}
    
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search