skip to Main Content

I’m really puzzled and can’t understand why my {{ time }} returns an empty value.

I’m trying to calculate the difference between the time currently and a booking time in Shopify. For the booking I have the date (first line_item.property) and the time (second one) coming separately. I want to add one to another and then compare it to the current time, but before I can do the math I have tried to display the different variables I have created.
{{ now }} & {{ date }} work fine but for {{ time }} it returns an empty value. On the other hand if instead of assigning it a value name I just display the line.item.property it works just fine.

Can you help me understand what I am doing wrong here?

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

2

Answers


  1. Chosen as BEST ANSWER

    Thanks Charles that really unblocked me. Posting here the final version I went with as I needed to compare exact time and not simply the date of the order.

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

  2. By using plus: 0 you can convert the string to integer, this will enable math operation for your comparison.

      {% assign today = 'now' | date: '%s' | plus: 0 %}
    

    check with {% unless line_item.properties == empty %} to see if properties exist.

    {% assign date = property.last | date: '%s' %} , the variable property.last must follow a well-formatted dates for date to work. liquid-date-format

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

    problem with offset:1, if the array only has 1 line_item.properties this will not run at all. Hence time is empty; or it exist but property.last does not have a date format.

    {% 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}}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search