djfidelio
djfidelio

Reputation: 63

Shopify Liquid finding out date of a specific day of the week

Wondering if someone could help with a question on Liquid / Shopify. I am working with a store that delivers product every Thursday, provided you order by Tuesday 12pm of the same week.

In the notification to the customer, I needed to indicate which Thursday it will ship i.e. 'This' or 'Next' based on order date. I have accomplished that with the following logic in liquid:

{% if  date | date: "%A" == 'Tuesday' and date | date: "%k" > 12 %}
   Your order will be delivered on Thursday of next week.
{% elsif date | date: "%A" == 'Wednesday' or date | date: "%A" == 'Thursday' %}
    Your order will be delivered on Thursday of next week.
{% else %}
    Your order will be delivered this Thursday
{% endif %}

Above does the job for the moment. However, I've been tasked to actually publish the 'date' of the Thursday of delivery. I've racked my brains and tried to figure out a way where I can write a function in liquid to tell me what 'This' Thursdays date is and what 'Next' Thursdays date will be. But I'm coming up trumps. I know how to do this in PHP and Ruby using the inbuilt functions but I'm new to Liquid and don't think it has inbuilt functions because it's a markup language (. So I would think you'd have to do some unix time math, but that makes it really complex.

Is there another way, or do I have to resort to using some convoluted unix timestamp math?

Thanks.

Upvotes: 5

Views: 6422

Answers (1)

drip
drip

Reputation: 12933

You will be required to make some unix calculations, but they will be simple ones.

See the code below:

{% assign today = 'now' | date: '%s' %}
{% assign single_day = 86400 %}
{% assign today_number = 'now' | date: '%u' | plus: 0 %}

{% if  today_number < 2 %}  

    Delivery by {{ today | plus: single_day | date: '%Y/%m/%d' }}

{% else %}

    {% assign days_until_next = 9 | minus: today_number  %}
    {% assign days_unix = days_until_next | times: single_day  %}
    Delivery by {{ today | plus: days_unix  | date: '%Y/%m/%d' }}

{% endif %}

I will try to break it down.

Assigning reusable data

{% assign today = 'now' | date: '%s' %} - with this we are getting the current unix timestamp for the server date

{% assign single_day = 86400 %} - this is the number that defines a single day in unix timestamp

{% assign today_number = 'now' | date: '%u' | plus: 0 %} - this returns the current day as a number (1...7). The plus: 0 is to convert it to a number instead of a string.

Condition

{% if today_number < 2 %} - we are checking if today_number is smaller than 2 ( where 2 is Tuesday ), it can be written in this case like so as well {% if today_number == 1 %}, but you get the logic

Output

If the above condition is true we do this:

{{ today | plus: single_day | date: '%Y/%m/%d' }} - this sums the timestamp today and the single_day timestamp, giving us the date tomorrow

If the if statement return false we go in the {% else %} statement where we set a few variables.

{% assign days_until_next = 9 | minus: today_number %} - since we are counting from the 2nd day I'm using the number 9 as a base and substract the current today_number so for example if today is Tuesday, we have 2 and 9-2=7, so this means the next Tuesday is 7 days from now.

{% assign days_unix = days_until_next | times: single_day %} - this just calculates the single day unix number times the days until the next Tuesday. So if we continue the above Tuesday example you get 86400*7=604800

{{ today | plus: days_unix | date: '%Y/%m/%d' }} - finally we sum the today unix number with the unix number for the days until the next Tuesday and we convert it to a readable date.

Hope that this is simple enough to follow.

Have a great day!

Upvotes: 10

Related Questions