Why is product.id and item.product.id complete diiferent when i print on cart?

I tried to add the below code to check if Product id and item id are the same, but these both are completely different when I print.

{% for product in collections.all.products %}
   {% for item in cart.items %}
            {{ product.id }} | {{ item.product.id }}
     {% if product.id == item.product.id %}
            Success
{% endif %}
{% endfor %}

I am getting 2 different ID when i print.

Thanks

Probably because you are looping the collections.all.products collection. Looping through collections has a limit of 50 items, so if you have more than 50 products on your store, you might never hit the match you’re looking for.

Not sure of your use case, but it seems like all this loop would do is check if the product in the cart is in the all collection. Since you are checking this against all items in the store, this would always be true?

If you mean for {{ product.id }} | {{ item.product.id }} you are printing EVERY line-item after all.

What’s the behavior when the test output is in the comparison conditional.

Also the code snippet is missing a closing {% endfor %} so make sure that’s not in the main code too.