d
Topic
Posts:
89
Last edited April 23, 2010

Comparing numbers with string variables in Liquid This post is outdated

Forgive me if this has been extensively discussed elsewhere but a search didn't turn anything up.

I have to print a special message on a client's cart page if the total quantity of a certain kind of product exceeds a certain number.

It won't do to simply check cart.item_count, because not every product should count toward the quantity limit.

Ideally I would be able to do something like this, assuming that I need to display a special message if the user has selected 20 or more items with the word 'orange' in their title:

{% assign total_orange_items = '0' %}

{% for item in cart.items %}
    {% if item.title contains "orange" %}
        {% capture temp_total %}{{ total_orange_items | plus:item.quantity }}{% endcapture %}
        {% assign total_orange_items = temp_total %}
    {% endif %}
{% endfor %}

{% if total_orange_items >= 20 %}
    <p id='20-orange-warning'>blah blah blah</p>
{% endif %}

But of course that last 'if' statement will fail with:

Liquid error: comparison of String with 20 failed

So here's the workaround to compare a string variable to a literal number:

{% capture difference %}{{ total_orange_items | minus:20 }}{% endcapture %}

{% unless difference contains '-' %}
    <p id='20-orange-warning'>blah blah blah</p>
{% endunless %}

Pretty simple really, but it took some searching and thinking to come up with. Hope it helps someone!

 

 

http://digitalcraftworks.com
i
Replies
Posts:
5770
Last edited April 23, 2010

LOvely.

 

Before there were math filters, I was using this method:

{% assign total_orange_items = '' %}

{% for item in cart.items %}
    {% if item.title contains "orange" %}
        {% capture temp_total %}{{ total_orange_items }}*{% endcapture %}
        {% assign total_orange_items = temp_total %}
    {% endif %}
{% endfor %}

{% if total_orange_items.size >= 20 %}
    <p id='20-orange-warning'>blah blah blah</p>
{% endif %}
Caroline from http://11heavens.com ∴ mllegeorgesand AT gmail DOT com
Posts:
5770
Last edited April 23, 2010

rather this:

{% assign total_orange_items = '' %}

{% for item in cart.items %}
    {% if item.title contains "orange" %}
        {% for i in (1..item.quantity) %}
            {% capture temp_total %}{{ total_orange_items }}*{% endcapture %}
            {% assign total_orange_items = temp_total %}
        {% endfor %}
    {% endif %}
{% endfor %}

{% if total_orange_items.size >= 20 %}
    <p id='20-orange-warning'>blah blah blah</p>
{% endif %}
Caroline from http://11heavens.com ∴ mllegeorgesand AT gmail DOT com
Posts:
5770
April 23, 2010

that was painful...

Caroline from http://11heavens.com ∴ mllegeorgesand AT gmail DOT com
Posts:
89
April 23, 2010

Yes, I found this thread about an hour ago and was considering that method, but I really wanted to avoid the inner for-loop. Thank goodness for the math filters!

http://digitalcraftworks.com
Posts:
5770
April 23, 2010

yes, thank god for them!

Caroline from http://11heavens.com ∴ mllegeorgesand AT gmail DOT com
Jamie Chief Officer of Funness shopify.com/plus
Posts:
5854
April 24, 2010

 

{% capture difference %}{{ total_orange_items | minus:20 }}{% endcapture %}

{% unless difference contains '-' %}
    <p id='20-orange-warning'>blah blah blah</p>
{% endunless %}

This is lovely Ben! I love simple snippets like this. Thanks for posting the hint for others!

 

http://evolvedesign.co ::: http://twitter.com/bacchus
sxalexander Shopify Partner
Posts:
2
March 10, 2011

This saved me!  It probably belongs in the Liquid docs.

Jamie Chief Officer of Funness shopify.com/plus
Posts:
5854
March 10, 2011

Yeah that's a good little snippet. Glad it saved you. ;)

http://evolvedesign.co ::: http://twitter.com/bacchus

Log in or sign up for an account to reply.

This thread has been closed! You will not be able to reply.