Shopify 调试液

Debug liquid for Shopify

我真的很困惑,无法理解为什么我的 {{ time }} returns 是一个空值。

我正在尝试计算当前时间与 Shopify 中的预订时间之间的差异。对于预订,我有单独的日期(第一个 line_item.property)和时间(第二个)。我想将一个与另一个相加,然后将其与当前时间进行比较,但在我进行数学运算之前,我已经尝试显示我创建的不同变量。 {{ now }} & {{ date }} 工作正常,但对于 {{ time }} 它 returns 是一个空值。另一方面,如果我不给它分配一个值名称,而是只显示 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 才能工作。 liquid-date-format

{% for property in line_item.properties offset:1 %}
        {% assign time = property.last | date: '%s' %}
      {% endfor %}

offset:1 的问题,如果数组只有 1 个 line_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}}

感谢 Charles 帮我解封了。 在这里发布我使用的最终版本,因为我需要比较确切的时间而不仅仅是订单日期。

      {% assign now_date = 'now' | date: '%s' | plus: 0 %}
  {% assign now_time = 'now' | date: '%s' | plus: 0 %}
    {% for order in customer.orders %}
        {%- for line_item in order.line_items -%}
            {% for property in line_item.properties limit:1 %}
                {% assign booking_date = property.last | date: '%s' | plus: 0 %}
            {% endfor %}
            {% for property in line_item.properties offset:1 limit:1 %}
                {% assign booking_time = property.last | date: '%s' | plus: 0 %}
            {% endfor %}
            {% assign date_to_compare = booking_date | plus: '86400' %}
                {% unless now_date > date_to_compare %}
                  {% if now_time <= booking_time or now_date <= booking_date %}SEANCE A VENIR{% endif %}            
                {% endunless %}
        {%- endfor -%}
    {% endfor %}