Showing max delivery date on order confirmation email

Solved
RickBJJFG
Tourist
3 0 1

Hello,

We're making use of backorders in our webshop and want to make the delivery time as clear as possible for all our customers.

I've been trying to write some code to show our customers in their order confirmation email within how many days they may expect their package to be delivered. This is based on what tag the product has and which country the order gets shipped to.

Here is the code I have at the moment:

{% for item in line_items %}
{% if shipping_address.country == 'Netherlands' or shipping_address.country == 'Belgium' %}
{% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 12 working days</b>
{% elsif item.product.tags contains 'atamabackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 10 working days</b>
{% elsif item.product.tags contains 'ggbackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 9 working days</b>
{% elsif item.product.tags contains 'tatamibackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 5 working days</b>
{% endif %}
{% elsif shipping_address.country == 'Denmark' or shipping_address.country == 'Germany' or shipping_address.country == 'Austria' or shipping_address.country == 'Switzerland' %}
{% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 13 working days</b>
{% elsif item.product.tags contains 'atamabackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 11 working days</b>
{% elsif item.product.tags contains 'ggbackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 10 working days</b>
{% elsif item.product.tags contains 'tatamibackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 6 working days</b>
{% endif %}
{% elsif shipping_address.country == 'United States' or shipping_address.country == 'France' or shipping_address.country == 'Spain' or shipping_address.country == 'Italy' or shipping_address.country == 'United Kingdom' or shipping_address.country == 'Malta' or shipping_address.country == 'Croatia' %}
{% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 14 working days</b>
{% elsif item.product.tags contains 'atamabackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 12 working days</b>
{% elsif item.product.tags contains 'ggbackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 11 working days</b>
{% elsif item.product.tags contains 'tatamibackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 7 working days</b>
{% endif %}
{% elsif shipping_address.country == 'Ireland' or shipping_address.country == 'Portugal' or shipping_address.country == 'Sweden' %}
{% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 15 working days</b>
{% elsif item.product.tags contains 'atamabackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 13 working days</b>
{% elsif item.product.tags contains 'ggbackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 12 working days</b>
{% elsif item.product.tags contains 'tatamibackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 8 working days</b>
{% endif %}
{% elsif shipping_address.country == 'Finland' %}
{% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 16 working days</b>
{% elsif item.product.tags contains 'atamabackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 14 working days</b>
{% elsif item.product.tags contains 'ggbackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 13 working days</b>
{% elsif item.product.tags contains 'tatamibackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 9 working days</b>
{% endif %}
{% else %}
{% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 21 working days</b>
{% elsif item.product.tags contains 'atamabackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 19 working days</b>
{% elsif item.product.tags contains 'ggbackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 18 working days</b>
{% elsif item.product.tags contains 'tatamibackorder' %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within 14 working days</b>
{% endif %}{% endif %}{% endfor %}

This partly works, but it currently seems to go through the first product in the order confirmation, to check if that one has any of the tags and then returns the value of that. What I need to have is that it prioritizes the highest delivery date. I wrote the code in order of prioritization, but this doesn't work because it checks the first product for all tags first, while I need it to check the first tags in all products and then stop once it finds the first in one of the products.

I hope my explanation is clear enough and would be extremely grateful if someone could help me out!

0 Likes
pvoulg
Shopify Partner
142 23 41

Hi @RickBJJFG

First off I'd start by optimizing your code a little bit. There's no need to repeat the phrase every time, since most of it remains the same. You can just use it once, after your for loop and assign a variable for the number of days, which is the only thing that effectively changes. 

So that part could be:

{% for item in line_items %}
Run your if statements here and get a variable
{% endfor %}

<b style="color:#ce2219">Place your sentence with the variable here</b>

 

Now, on how you could go about finding that variable. I've simplified your code for the first part (for brevity's sake), but you can practically copy it all the way through the rest of your if statements. So what we're going after is assigning a day parameter for each if, gathering them all at the end and appending them on an existing array (so that we can gather the days from all the products and sort them later). It goes a bit like this: 

{% capture days %}
{% for item in line_items %}
{% if shipping_address.country == 'Netherlands' or shipping_address.country == 'Belgium' %}
  {% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
    {% assign day = 12 %}
  {% elsif item.product.tags contains 'atamabackorder' %}
    {% assign day = 10 %}
  {% elsif item.product.tags contains 'ggbackorder' %}
    {% assign day = 9 %}
  {% elsif item.product.tags contains 'tatamibackorder' %}
    {% assign day = 5 %}
  {% endif %}
{% endif %}
{{ day }}    
{% if forloop.last == false %},{% endif%}
{% endfor %}
{% endcapture %}

{% assign number_of_days = days | split: ',' | sort  %}	

<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within {{ number_of_days.last }} working days</b>

 

Now, granted it's a bit late, and appending to arrays in liquid is always a pain, I believe the above should work. If not let me know and I'll have another look. 

Panos Voulgaris
Creative director / Partner @ MALVI
Founder @ function( )
0 Likes
RickBJJFG
Tourist
3 0 1

Hi @pvoulg 

Thank you so much for your reply!

Learned a lot already from your optimized code! It doesn't quite work yet though. It seems to still take the lowest number of days and return that. I don't know if that's because it sorts and takes the lowest number, or if that's the case when the first product in the cart/confirmation mail is the one with the lowest number and it just takes that one and doesn't look for the highest among all products.

Also what I forgot to mention, we do not only sell backorder items, so the sentence should only display if one of the products contains one of the tags. I tried to do this myself in a somewhat optimized way by using the amount of days, so that we don't need to use the for statement again, I don't know if that's a good choice though as it doesn't seem to work either.

{% capture days %}
{% for item in line_items %}
{% if shipping_address.country == 'Netherlands' or shipping_address.country == 'Belgium' %}
  {% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
    {% assign day = 12 %}
  {% elsif item.product.tags contains 'atamabackorder' %}
    {% assign day = 10 %}
  {% elsif item.product.tags contains 'ggbackorder' %}
    {% assign day = 9 %}
  {% elsif item.product.tags contains 'tatamibackorder' %}
    {% assign day = 5 %}
  {% endif %}
{% endif %}
{{ day }}
{% if forloop.last == false %},{% endif%}
{% endfor %}
{% endcapture %}

{% assign number_of_days = days | split: ',' | sort  %}	

{% if number_of_days > 2 %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within {{ number_of_days.last }} working days</b>
{% endif %}
0 Likes
pvoulg
Shopify Partner
142 23 41

This is an accepted solution.

Hey @RickBJJFG

Well yeah, it figures. As I mentioned, it was a bit late, so my brain didn't quite take into account that I was trying to sort a string, instead of numbers. Therefore, it was sorting them in this fashion: 10, 12, 5, 9 instead of the correct incrementing numerical order of: 5, 9, 10, 12

So, after an extra step of converting strings into numbers and comparing them properly, you get this:

{% capture days %}
{% for item in line_items %}
{% if shipping_address.country == 'Netherlands' or shipping_address.country == 'Belgium' %}
  {% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
    {% assign day = 12 %}
  {% elsif item.product.tags contains 'atamabackorder' %}
    {% assign day = 10 %}
  {% elsif item.product.tags contains 'ggbackorder' %}
    {% assign day = 9 %}
  {% elsif item.product.tags contains 'tatamibackorder' %}
    {% assign day = 5 %}
  {% endif %}
{% endif %}
{{ day }}
{% if forloop.last == false %},{% endif%}
{% endfor %}
{% endcapture %}

{% assign number_of_days = days | split: ',' %}
{% assign most_days = number_of_days | first | plus: 0 %}

{% for the_day in number_of_days %}
    {% assign cur_number = the_day | plus: 0 %}
    {% if cur_number >= most_days %}
      {% assign most_days = the_day | plus: 0 %}
    {% endif %}
{% endfor %}

{% if most_days > 2  %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within {{ most_days }} working days</b>
{% endif %}

 

I added your comparison as well (most_days > 2), so check it out and let me know if it works for you.

Panos Voulgaris
Creative director / Partner @ MALVI
Founder @ function( )
RickBJJFG
Tourist
3 0 1

Thank you so much @pvoulg !

This works perfectly. I managed to finish the code for the rest of the countries myself, tested it and it all seems to work just as it should!

{% capture days %}
{% for item in line_items %}
{% if shipping_address.country == 'Netherlands' or shipping_address.country == 'Belgium' %}
  {% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
    {% assign day = 12 %}
  {% elsif item.product.tags contains 'atamabackorder' %}
    {% assign day = 10 %}
  {% elsif item.product.tags contains 'ggbackorder' %}
    {% assign day = 9 %}
  {% elsif item.product.tags contains 'tatamibackorder' %}
    {% assign day = 5 %}
  {% endif %}

{% elsif shipping_address.country == 'Denmark' or shipping_address.country == 'Germany' or shipping_address.country == 'Austria' or shipping_address.country == 'Switzerland' %}
  {% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
    {% assign day = 13 %}
  {% elsif item.product.tags contains 'atamabackorder' %}
    {% assign day = 11 %}
  {% elsif item.product.tags contains 'ggbackorder' %}
    {% assign day = 10 %}
  {% elsif item.product.tags contains 'tatamibackorder' %}
    {% assign day = 6 %}
  {% endif %}

{% elsif shipping_address.country == 'United States' or shipping_address.country == 'France' or shipping_address.country == 'Spain' or shipping_address.country == 'Italy' or shipping_address.country == 'United Kingdom' or shipping_address.country == 'Malta' or shipping_address.country == 'Croatia' %}
  {% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
    {% assign day = 14 %}
  {% elsif item.product.tags contains 'atamabackorder' %}
    {% assign day = 12 %}
  {% elsif item.product.tags contains 'ggbackorder' %}
    {% assign day = 11 %}
  {% elsif item.product.tags contains 'tatamibackorder' %}
    {% assign day = 7 %}
  {% endif %}

{% elsif shipping_address.country == 'Ireland' or shipping_address.country == 'Portugal' or shipping_address.country == 'Sweden'  %}
  {% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
    {% assign day = 15 %}
  {% elsif item.product.tags contains 'atamabackorder' %}
    {% assign day = 13 %}
  {% elsif item.product.tags contains 'ggbackorder' %}
    {% assign day = 12 %}
  {% elsif item.product.tags contains 'tatamibackorder' %}
    {% assign day = 8 %}
  {% endif %}

{% elsif shipping_address.country == 'Finland' %}
  {% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
    {% assign day = 16 %}
  {% elsif item.product.tags contains 'atamabackorder' %}
    {% assign day = 14 %}
  {% elsif item.product.tags contains 'ggbackorder' %}
    {% assign day = 13 %}
  {% elsif item.product.tags contains 'tatamibackorder' %}
    {% assign day = 9 %}
  {% endif %}

{% else %}
  {% if item.product.tags contains 'kingzbackorder' or item.product.tags contains 'progressbackorder' %}
    {% assign day = 21 %}
  {% elsif item.product.tags contains 'atamabackorder' %}
    {% assign day = 19 %}
  {% elsif item.product.tags contains 'ggbackorder' %}
    {% assign day = 18 %}
  {% elsif item.product.tags contains 'tatamibackorder' %}
    {% assign day = 14 %}
  {% endif %}

{% endif %}

{{ day }}
{% if forloop.last == false %},{% endif%}
{% endfor %}
{% endcapture %}

{% assign number_of_days = days | split: ',' %}
{% assign most_days = number_of_days | first | plus: 0 %}

{% for the_day in number_of_days %}
    {% assign cur_number = the_day | plus: 0 %}
    {% if cur_number >= most_days %}
      {% assign most_days = the_day | plus: 0 %}
    {% endif %}
{% endfor %}

{% if most_days > 2  %}
<b style="color:#ce2219">Your order contains one or more products with a longer delivery time. You can expect to receive your order within {{ most_days }} working days</b>
{% endif %}

Huge thanks to you! Have a good one!

pvoulg
Shopify Partner
142 23 41

Glad I could help. Take care!

Panos Voulgaris
Creative director / Partner @ MALVI
Founder @ function( )