How to hide a tag on a Shopify product page if certain condition is met? - shopify

A liquid logic is calculating the highest score (tag) and outputting the result on the product page. However, the same list of scores (tags) is also being displayed somewhere else on the page. I'd like to be able to hide the highest score in the list of scores as this is already showing in a separate box.
Here is my logic calculating the highest score:
{%- capture list_of_scores -%}
{{wa}}|Wine Advocate,
{{bh}}|Burghound,
{{ag}}|Vinous,
{{jr}}|Jancis Robinson,
{{jg}}|John Gilman
{%- endcapture -%}
{%- capture list_of_scores_num -%}
{{wa}},
{{bh}},
{{ag}},
{{jr}},
{{jg}}
{%- endcapture -%}
{% assign scores_array = list_of_scores | split: ',' %}
{% assign scores = list_of_scores_num | split: ',' %}
{% assign highest_score = scores | first | plus: 0 %}
{% assign name = '' %}
{% for score_val in scores %}
{% assign cur_score = score_val | plus: 0 %}
{% if cur_score >= highest_score %}
{% assign highest_score = score_val | plus: 0 %}
{% assign name = scores_array[forloop.index0] | split: '|' | last %}
{% endif %}
{% endfor %}
<span>{{ highest_score }}</span>
<h5>{{ name }}</h5>
I tried this to hide the highest tag within the list:
{% if wa != highest_score %}<span>WA {{ wa }}</span>{% endif %}
Any suggestions?

{% if wa != highest_score %} this condition will always be true as it seems that wa is a string and highest_score is an integer (as | plus: 0 is used to cast a string to an integer). An integer cannot be equal to a string.
You can try to also cast wa to an integer and then compare it to highest_score :
{% assign waToInt = wa | plus: 0 %}
{% if waToInt != highest_score %}<span>WA {{ wa }}</span>{% endif %}

Related

Go through all available variants in Shopify liquid

I am trying to show all the available sizes from a product in the collection page.
I managed to write this snippet that will show available sizes, but only for one of the color combinations.
{% for option in product.options_with_values %}
{% assign downcased_option = option.name | downcase %}
{% if downcased_option contains 'size' %}
{% assign is_size = true %}
{% for value in option.values %}
{% assign variant_available = true %}
{% if product.options.size >= 1 %}
{% unless product.variants[forloop.index0].available %}
{% assign variant_available = false %}
{% endunless %}
{% endif %}
<span class="{% unless variant_available %}soldout{% endunless %}">{{ value | escape }}</span>
{% endfor %}
{% endif %}
{% endfor %}
But, let's say we have the next variants:
Green/32, Green/34
Blue/34
Black/34, Black/36
With the snippet above, it will not show available size 36 from black color as it's not available in green.
What I want is to go through every single size and show if availability, no matter the color.
Does someone know how to achieve this?
Thanks
Forgive me if I'm missing something but making the assumption that all variants have a size and there are only two options you just need to loop through the available variants and just display the variant name split it and display first or last where ever the size is in the options list. if there are loads of duplicates then put them in an array and run 'uniq'.
{% for variant in product.variants %}
{% if variant.available %}
{% assign sizesList = variant.title | split: '/' | first | append: ', ' }}
{% endif %}
{% endfor %}
{% assign sizes = sizesList | split: ', ' %}
{{ sizes | uniq | join: ", " }}

How to fetch unique value in liquid file

here is my code i tried to fetch only unique value from items variable but I got all value can someone please help to solve this
{% capture items %}
{% for tag in product.tags %}
{%if tag contains 'pair'%}
{% assign tag_split = tag | remove_first: 'pair::' | split: "::" %}
{% assign color = tag_split[0] %}
{{color}}
{% endif %}
{% endfor %}
{% endcapture %}
{% assign my_array = items | split: ", " %}
<p>{{ my_array | uniq }} </p>
This filter should help you to keep only one elem per value:
https://shopify.github.io/liquid/filters/uniq/

Debug liquid for Shopify

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

How to output highest score and name associated with it - liquid logic

I need to output the highest_score and the name associated with it. The code outputs the highest_score but it's not associated with the right name. I guess it needs a for within a for loop, but I'm not quite sure how to construct it.
Here's my code:
{%- capture list_of_scores -%}
{{wa}}|Wine Advocate,{{bh}}|Burghound,{{ag}}|Vinous,{{jr}}|Jancis Robinson,{{jg}}|John Gilman
{%- endcapture -%}
{%- capture list_of_scores_num -%}{{wa}},{{bh}},{{ag}},{{jr}},{{jg}}{%- endcapture -%}
{% assign scores_array = list_of_scores | split: ',' %}
{% assign scores = list_of_scores_num | split: ',' %}
{% assign highest_score = scores | first | plus: 0 %}
{% for score_val in scores %}
{% assign cur_score = score_val | plus: 0 %}
{% if cur_score >= highest_score %}
{% assign highest_score = score_val | plus: 0 %}
{% endif %}
{% endfor %}
{% for score_and_name in scores_array %}
{% assign split_score_and_name = score_and_name | split: '|' %}
{% assign score = split_score_and_name[0] %}
{% assign score = highest_score %}
{% assign name = split_score_and_name[1] %}
{% endfor %}
<span>{{ highest_score }}</span>
<h5>{{ name }}</h5>
Thanks
You can do something like this
{% assign wa = 12 %}
{% assign bh = 16 %}
{% assign ag = 26 %}
{% assign jr = 6 %}
{% assign jg = 11 %}
{%- capture list_of_scores -%}
{{wa}}|Wine Advocate,
{{bh}}|Burghound,
{{ag}}|Vinous,
{{jr}}|Jancis Robinson,
{{jg}}|John Gilman
{%- endcapture -%}
{%- capture list_of_scores_num -%}
{{wa}},
{{bh}},
{{ag}},
{{jr}},
{{jg}}
{%- endcapture -%}
{% assign scores_array = list_of_scores | split: ',' %}
{% assign scores = list_of_scores_num | split: ',' %}
{% assign highest_score = scores | first | plus: 0 %}
{% assign name = '' %}
{% for score_val in scores %}
{% assign cur_score = score_val | plus: 0 %}
{% if cur_score >= highest_score %}
{% assign highest_score = score_val | plus: 0 %}
{% assign name = scores_array[forloop.index0] | split: '|' | last %}
{% endif %}
{% endfor %}
<span>{{ highest_score }}</span>
<h5>{{ name }}</h5>
The main difference is that we moved the name value outside of the loop as an empty variable and inside the loop that we check the highest number we assign the name variable using the forloop.index0 using the following code:
{% assign name = scores_array[forloop.index0] | split: '|' | last %}
So we need only 1 loop.

Shopify: calling swatch.liquid 'colors' on collection.liquid and product-loop.liquid

I have not been able to find a fix for my issue. I want to show a simple "More Colors" option beneath the product prices.
Screenshot of Product example
I have been testing code such as:
{% if product.variants 'color' > 1 %}
Has more than one variant.
{% else %}Only one variant.
{% endif %}
But haven't had any luck. Everything either calls "Has more..." or "Only one..." regarless of the swatch count.
I am editing product-loop.liquid which is being called from collections.liquid
It has only outputed "Only one variant" or "Has more than one variant" for every product regardless of swatch/color count.
Thanks for any help..
This statement is nonsense:
{% if product.variants 'color' > 1 %}
Instead, you want to check the options set for your variants, and if you detect an option set to color, and you have more than one variant, then clearly, you are in more than one color territory.
{% assign has_more_colors = false %}
{% for option in product.options %}
{% if option contains 'color' and product.variants.length > 1 %}
{% assign has_more_colors = true %}
{% endif %}
{% endfor %}
So now you can do whatever it is you need to do... since you now know you a) have an option called colors, and b) more than one variant, hence more than one color...
The following snippet was posted to the Shopify boards. It solved this issue:
{% assign option_title = "Color" %}
{% assign option_index = "" %}
{% assign option_values = "" %}
{% for option in product.options %}
{% if option == option_title %}
{% assign option_index = forloop.index %}
{% endif %}
{% endfor %}
{% for variant in product.variants %}
{% assign variant_option_value = "" %}
{% if option_index == 1 %}
{% assign variant_option_value = variant.option1 | handleize %}
{% elsif option_index == 2 %}
{% assign variant_option_value = variant.option2 | handleize %}
{% elsif option_index == 3 %}
{% assign variant_option_value = variant.option3 | handleize %}
{% endif %}
{% assign option_values = option_values | append:"," | append:variant_option_value %}
{% endfor %}
{% assign option_values = option_values | remove_first:"," | split:"," | uniq %}
{% if option_values.size > 1 %}
MORE COLORS
{% else %}
{% endif %}