问题描述
我正在尝试根据正在使用的本地域来显示shopify内容。为此,我有下面的方法可以正常工作:
{% if request.host contains 'co.nz' %}
{% assign site_currency = "NZ" %}
{% endif %}
问题在于,变量似乎仅在编译之前在一个片段/模板/布局文件中起作用,而Shopify类作为全局变量实际上只是主题设置中的一个值,因此不能有条件的。
我已经尝试过'{%Assign scope ='page'%}',该功能显然在编译后就可以使用,但是我无法使其正常运行,而且文档很少。
有人知道我怎样才能一次声明此变量,然后在整个主题中引用它?
谢谢!
解决方法
Shopify全局变量只是此处指定的对象-https://shopify.dev/docs/themes/liquid/reference/objects
只有通过JS将其存储在cart.attributes
中,才能实现全局引用功能而不通过片段进行引用,但这需要刷新页面才能生效。
创建代码段,或将代码粘贴到{{ content_for_layout }}
中的theme.liquid
上方
{% comment %} /snippets/global-vars.liquid {% endcomment %}
{% if request.host contains 'co.nz' %}
{% assign site_currency = "NZ" %}
{% endif %}
如果您使用几种不同的货币,建议您使用case/when
,here are这些文档。
{% comment %} /layout/theme.liquid {% endcomment %}
...
{% include 'global-vars' %}
{% comment %} this should print the currency {% endcomment %}
{{ site_currency }}
{{ content_for_layout }}
...