Ad

How To Hide A Tag On A Shopify Product Page If Certain Condition Is Met?

- 1 answer

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?

Ad

Answer

{% 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 %}
Ad
source: stackoverflow.com
Ad