Ad

Get An Array Of Product.id That Are In The Cart

- 1 answer

I'm trying to cycle over all my product.id in my cart. I have a various product with the same product.id but different line_item.

Example:

line.item[1]: productID 2756695130141

line.item[2]: productID 2756695130141

line.item[3]: productID 2756702765085

line.item[4]: productID 2756695130141

I want to know how many different productID I have (in this case, 2). How can I do this in Liquid/Shopify?

Ad

Answer

Best way to do that is to use arrays. Because they have map and unique.

This code will return array of unique product IDs

{% assign uniqueProductIdsArray= cart.items | map: 'product_id'| uniq  %}

This code will return concatenated string of unique product IDs

{% assign uniqueProductIdsString= cart.items | map: 'product_id'| uniq | join: ', ' %}

Documentation about Array filters in liquid: https://help.shopify.com/themes/liquid/filters/array-filters

Ad
source: stackoverflow.com
Ad