我正在尝试创建一个类似“预购”的机制,其中我的 Shopify Liquid 模板的某些元素仅在当前日期大于或小于元字段中指定的日期时显示。
截至目前,这就是我所拥有的,包括逻辑:
<!-- Check Today is correct -->
<p>Today: {{'now' | date: '%d-%m-%Y' }}</p>
<!-- This is the Metafield Output as a String -->
<p>Release Date: {{ product.metafields.Release-Date.preOrder }}</p>
<!-- Assign Variable "today_date" to the current date -->
{% assign today_date = 'now' | date: '%d-%m-%Y' %}
<!-- Assign Variable "pre_date" to the string of the metafield -->
{% assign pre_date = product.metafields.Release-Date.preOrder %}
{% if today_date > pre_date %}
Today's date is greater than PreOrder Date
{% else %}
Today's date is not greater than PreOrder Date
{% endif %}
但是,即使我将预购日期设置为 01-01-2018,它仍然显示“大于”。
我如何正确查询这个?
您不能以这种方式比较字符串。 (日期是字符串。)
你必须使用%s
而是使用日期过滤器。
所以它会变成:
{% assign today_date = 'now' | date: '%s' %}
{% assign pre_date = product.metafields.Release-Date.preOrder | date: '%s' %}
{% if today_date > pre_date %}
We use %s
因为它将返回当前的 unix 时间戳数字而不是字符串。这样您就可以比较不同的时间戳。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)