Count all products quantity in checkout

Solved
webg
Tourist
8 1 0

Hi. I am trying to count all the products quantity/variants that are visible in the checkout page.

 

Below, is that code that I currently have:

{% assign itemCount = 0 %}

{% for item in checkout.line_items %}
  {% assign itemCount = itemCount | plus: 1 %}
{% endfor %}

{{ itemCount }}

 

The code counts all the products, but if one of the products has quantity more than 1, it will still count as 1, not 2.

Here is an example, where the count should be equal with 4:

Screenshot 2020-09-15 at 16.18.39.png

 

Any help is appreciated!

Thank you

0 Likes

Hello,

Firstly are you working on a Shopify Plus store? Only with the Plus plan you can edit the checkout.

Kind regards,
Diego

◦ Problem Solved? ✔️ Please click on Accept as Solution and like.
◦ Using a free theme? Improve it with copy/paste code snippets clicking here.
◦ Looking to hire an experienced developer? Same day response: ezfycode@gmail.com.
0 Likes
webg
Tourist
8 1 0

Hi, yep. the screenshot you see is from the store checkout

0 Likes
webg
Tourist
8 1 0

Hi Diego, any chance you could help with this?

0 Likes
Ninthony
Shopify Partner
1626 224 611

This is an accepted solution.

Yo @webg, just got your DM. You need to use the current line_item.quantity and add it to the count every loop, but first you need to multiply it by 1 with a liquid math filter since it's initially a string and you cannot add it:

    {% assign total_item_count = 0 %}
    {% for item in checkout.line_items %}
      {% assign item_quantity = item.quantity | times: 1 %}
      {% assign total_item_count = total_item_count | plus: item_quantity %}
    {% endfor %}
    {{ total_item_count }}

 

If my solution helped you, please like and accept as solution!
If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
webg
Tourist
8 1 0

This is an accepted solution.

Yo Anthony.

 

yess! this solution works. thank you so much. you rock

0 Likes