繁体   English   中英

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

[英]Assigning conditional global liquid variables in Shopify

我正在尝试根据其使用的本地域显示 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中,但这需要页面刷新才能生效。

theme.liquid中创建一个片段或将代码粘贴到{{ content_for_layout }}上方

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

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

如果您有几种不同的货币,我建议您考虑使用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