简体   繁体   English

在 Shopify 中分配条件全局液体变量

[英]Assigning conditional global liquid variables in Shopify

I'm using trying to display shopify content based on the local domain it's using.我正在尝试根据其使用的本地域显示 shopify 内容。 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.问题是变量似乎只在编译之前在一个片段/模板/布局文件中起作用,而 Shopify 作为全局变量的类实际上只是主题设置中的一个值,因此不能是有条件的。

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.我试过 '{% assign scope = 'page' %}' 这显然在编译后有效但我无法让它工作并且文档很少。

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 Shopify 全局变量只是这里指定的对象 - 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.在不通过代码片段引用的情况下实现全局引用功能的唯一方法是通过 JS 将其存储在cart.attributes中,但这需要页面刷新才能生效。

Create a snippet or just paste the code above {{ content_for_layout }} in theme.liquidtheme.liquid中创建一个片段或将代码粘贴到{{ content_for_layout }}上方

{% 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.如果您有几种不同的货币,我建议您考虑使用case/when这里是文档。

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

...

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

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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