简体   繁体   中英

Shopify Liquid if statement

I use the Order Printer app in Shopify to print my orders. I have edited the the template to suit my needs, however I am quiet new to Liquid code.

Based on the shipping postcode of the order, I need the template to return 1 of 3 labels - Rural, Major and Outer. I have a list of postcodes in the following format (this is a small portion for example):

Rural

2648, 2715, 2717-2719, 2731-2739, 3221-3334, 3342-3349, 3351-3352, 3357-3426, 3444-3688, 3691-3749, 3812-3909, 3921-3925, 3945-3974, 3979, 3984-3999


Major

1000-1935, 2000-2079, 2085-2107, 2109-2156, 2158, 2160-2172, 2174-2229, 2232-2249, 2557-2559, 2564-2567, 2740-2744, 2747-2751, 2759-2764, 2766-2774, 2776-2777, 2890-2897


Outer

7020-7049, 7054, 7109-7150, 7155-7171, 7173-7247, 7255-7257, 7330-7799

I'm unable to work out how to use the if statement for the purpose of identifying if the shipping postcode is a rural , major or outer postcode, without typing out every postcode between 7330 and 7799 etc.

Can anyone help?

First declare your arrays:

{% assign Rural= "2648, 2715, 2717-2719, 2731-2739, 3221-3334, ...." | split: ", " %}
{% assign Major= "1000-1935, 2000-2079, 2085-2107, 2109-2156,..." | split: ", " %}
{% assign Outer= "7020-7049, 7054, 7109-7150, 7155-7171,...." | split: ", " %}

Then declare a variable that you will use for the label

{% assign relatedLabel = ""%}

Implement the if logic

{% if Rural contains Order.PosteCode %}
{% assign relatedLabel = "rural" %}
{% endif %}
{% if Major contains Order.PosteCode %}
{% assign relatedLabel = "major" %}
{% endif %}
{% if Outer contains Order.PosteCode %} 
{% assign relatedLabel = "outer" %}
{% endif %}

Finally you can print it where you need it

This Poste Code belongs to {{relatedLabel}} area.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM