1
votes

I'm trying to add 30 days to a pre-order date and if today's date is later, display a text string and if not display another text string. Any ideas where I'm going wrong?

{% assign assign pre_date = 259200 | plus: order.created_at | date: '%s' %}
{% assign today_date = 'now' | date: '%s' %}
{% if pre_date > today_date %}
disply this
{% else %}
this
{% endif %}
1
What do you get if you print the result of {% assign assign pre_date = 259200 | plus: order.created_at | date: '%s' %}? Operations are performed left-to-right, so it's trying to take your base offset and add whatever order.created_at is (which may not be a number, so may be returning 0), then converting that result to a date-in-seconds. I have a feeling that you want to change the order to order.created_at | date: '%s' | plus: 259200 so that you're definitely adding a number to a string-that-can-be-coerced-into-a-number. :) - Dave B
Also, since the date filter returns a string, you may need to coerce today_date to an integer by using {% assign today_date = 'now' | date: '%s' | times: 1 %} in order to get the comparison to work properly - Dave B
Thanks Dave, adding the times: 1 did the trick! - Craig D
Awesome! I'll add that as an actual answer so that you can mark this question solved :D - Dave B

1 Answers

0
votes

The date filter returns a string, even when you're using %s to get a number of seconds, so Shopify may be running into situations where you're comparing strings-that-look-like numbers instead of actual numbers

To coerce your variables into their proper numeric values, I find the simplest thing to do is to apply a neutral mathematical operation (either | plus: 0 or | times: 1)

So your final code might look something like:

{% assign pre_date = order.created_at | date: '%s' | plus: 259200 %}
{% assign today_date = 'now' | date: '%s' | times: 1 %}

{% if pre_date > today_date %}
  Pre-date is greater
{% else %}
  Today is the day
{% endif %}