Counting number of specific product type in cart

Kathy9
New Member
15 0 0

I have two product type "Tee" and "Jeans"

I will like to have gift wrap option if customer purchased 4 tee or more.

I noticed that cart.item_count can return to total number product in the cart. But I would like something like that be return on number of "Tee" only

Is there any variable that return the number of specific product type only?

Thanks in advance.

Replies 6 (6)
tetchi
Shopify Staff (Retired)
489 0 45

This is definitely possible with some Liquid magic. Let me write something up, and I will get back to you shortly.

- tetchi

www.tetchi.ca | www.twitter.com/t3tchi
tetchi
Shopify Staff (Retired)
489 0 45

Sweet! So I got it work on my test shop at: https://crona-llc4353.myshopify.com. 

Here's the code that I used - in my example I'm checking for the product type "kicks".

{% assign num = 0 %}     
{% for item in cart.items %}            
    {% if item.product.type == "kicks" %}       
         {% capture temp %}{{ num | plus: item.quantity }}{% endcapture %} 
    {% endif %}
    {% assign num = temp %}

... rest of cart content code here.

{% endfor %}

Number of kicks: {{ num }} 

You can see a live demo by adding kicks to the cart here: https://crona-llc4353.myshopify.com/collections/types?q=kicks

Screenshot of cart: http://take.ms/59A

www.tetchi.ca | www.twitter.com/t3tchi
Kathy9
New Member
15 0 0

Thank you, tetchi.

One more thing.

I am trying to something more like:

{% if num >= 4 %}

<p>Show some message</p>

{% else %}

<p>Show another message</p>

{% endif %}

But It returned:

Liquid error: comparison of String with 4 failed

Do you know the reason why?

tetchi
Shopify Staff (Retired)
489 0 45

Ah yes! The problem is explained in my blog entry here: http://www.tetchi.ca/liquids-capture-tag-and-numbers/

To fix this, you need to convert "num" into a number.

 

{% assign num = 0 %}     
{% for item in cart.items %}            
    {% if item.product.type == "kicks" %}       
         {% capture temp %}{{ num | plus: item.quantity }}{% endcapture %} 
    {% endif %}
    {% assign num = temp %}

... rest of cart content code here.

{% endfor %}

{% assign new_num = num | plus: 0 %}

{% if new_num >= 4 %}
 bla bla
{% endif %}

Cheers,

-tetchi

 

www.tetchi.ca | www.twitter.com/t3tchi
Kathy9
New Member
15 0 0

Thank you very much, tetchi.

You are my life saver.

 

qjames
Tourist
6 0 6

This is another way to do it - although, because shopify liquid doesn't contain an array sum function it's not better than the other solution offered as it involves more iterations - but it can be better in situations where you just need a filtered array without having to then compare it to the original to get the quanitity property:

 

{% assign tee_items = cart.items | map: "product" | where: "type", "Tee" %}

{% for tee_item in tee_items %}
    {% assign matching_line_item_quantities = cart.items | where: "title", tee_item.title | map: "quantity" %}

    {% for matching_line_item_qty in matching_line_item_quantities %}
        {% assign qty = qty | plus: matching_line_item_qty %}
    {% endfor %}

{% endfor %}