首页 > 解决方案 > 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 %}

标签: shopify

解决方案


通过使用plus: 0您可以将 转换stringinteger,这将为您的比较启用数学运算。

  {% 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}}

推荐阅读