Sorting cart items

New Member
1 0 0

Hi there,

i am using the debut theme and edited the cart-template.liquid code like the following doc to sort and reverse items in the cart:

          {% assign items = cart.items | sort: 'title' %}
          {% for item in items reversed %}

I am sorting and using reversed because all my items title start with "The..." except my free gift item which i add to the cart automatically. So that in the end my free gift item is on the last position in the cart like i want it.

However when i now add for example 3 items to the cart and delete one of those the wrong item will be removed because of the sorting. Any idea to fix that or do you have another idea to put one item in the cart always to the last positon?




Replies 3 (3)
Shopify Partner
15 0 0

Hello, You can use 'forloop.last' and exclude gift product from main for loop. 

    {% if forloop.last == true %}
        // the gift product here
    {% endif %}
Shopify Expert
10438 169 2081

Since your store is locked (and is unable to be viewed) we have to make some assumptions:

The reason why the wrong item is being removed is likely down to how the code on the cart page works on line numbers. Even though the order of items are reversed, the line numbers attached to them are not. This could mean you think you're deleting item 3, but end up deleting item 1 instead.

The code that removes or updates items isn't smart enough to know the order has been changed. You'd need to further adapt the code to handle the reversal.

I jump on these forums to help and share some insights. Not looking to be hired, and not looking for work.
Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.
★ ★
6 1 0


I think I’m having the same problem mentioned in this thread. On my side, I’ve been able to group products by vendor in the cart page. But I’ve noticed that for example the second item who appear in the cart page will have a data index of 5. So when I go to the checkout page the quantity of this item is not the same that is on the cart page.  And it happens randomly to all the products in my  cart. Do you a way I could approach this problem? I've attached a preview link of my store (


Thank you,