shopify - Shopify 的调试液
问题描述
我真的很困惑,不明白为什么我{{ time }}
返回一个空值。
我正在尝试计算当前时间与 Shopify 中的预订时间之间的差异。对于预订,我有日期(第一个 line_item.property)和时间(第二个)分别来。我想将一个添加到另一个,然后将其与当前时间进行比较,但在进行数学运算之前,我尝试显示我创建的不同变量。
{{ now }}
&{{ date }}
工作正常,但{{ time }}
它返回一个空值。另一方面,如果不是为它分配一个值名称,我只是显示line.item.property
它就可以了。
你能帮我理解我在这里做错了什么吗?
{% assign today = 'now' | date: '%s' %}
{% for order in customer.orders %}
{%- for line_item in order.line_items -%}
{% for property in line_item.properties limit:1 %}
{% assign date = property.last | date: '%s' %}
{% endfor %}
{% for property in line_item.properties offset:1 %}
{% assign time = property.last | date: '%s' %}
{% endfor %}
{%- endfor -%}
{{ now }} - {{ date }} - {{ time }}
{% endfor %}
解决方案
通过使用plus: 0
您可以将 转换string
为integer
,这将为您的比较启用数学运算。
{% assign today = 'now' | date: '%s' | plus: 0 %}
检查{% unless line_item.properties == empty %}
是否存在属性。
{% assign date = property.last | date: '%s' %}
,变量property.last
必须遵循格式正确的日期date
才能工作。液体日期格式
{% for property in line_item.properties offset:1 %}
{% assign time = property.last | date: '%s' %}
{% endfor %}
的问题offset:1
,如果数组只有 1line_item.properties
这根本不会运行。因此time
是空的;或者它存在但property.last
没有date
格式。
{% assign today = 'now' | date: '%s' | plus: 0 %}
{% for order in customer.orders %}
{%- for line_item in order.line_items -%}
{% unless line_item.properties == empty %}
{% for property in line_item.properties %}
{% if forloop.index == 1 %}
{% assign date = property.last | date: '%s' | plus: 0 %}
{% if date == 0 %}
{% comment %}Opp! Not A Date, Terminate loop{% endcomment %}
{% break %}
{% endif %}
{% else %}
{% assign time = property.last | date: '%s' | plus: 0 %}
{% unless time == 0 %}
{% assign date = date - time %}
{% endunless %}
{% endif %}
{% endfor %}
{% endunless %}
{% endfor %}
{{today - date}}
推荐阅读
- javascript - 带有 Intersection Observer API 问题的图像延迟加载
- python - Python - 使用 Beautifulsoup 从网页中提取数据
- r - 将用户定义的字符串替换函数应用于小标题的内容时遇到问题
- javascript - 平衡字符Javascript
- python - 在 connexion 中链接 yaml 文件时出错
- arrays - 如何将数组发送到函数中
- java - 在java中使用单栈实现队列
- c# - 什么是上下文?如何将嵌入对象发送到 webhook?
- facebook - 从私人 Facebook 群组中抓取电子邮件
- javascript - Firebase 函数:无法读取未定义的属性 https