Product Discount API - cannot sort line items by cost

Shopify Partner
2 0 0

I'm working with a client who wants to be able to apply different discounts to their items, sorted by descending price. For example: if there are 2 items in the cart, the cheapest item would be 20% off. If there are 3 items in the cart, the 2nd-cheapest item is 20% off and cheapest item is 30% off. If there are 4 items, then 20% off the second, 30% off third, 40% off fourth. And finally if there are 5 or more items in the cart, those discounts are applied AND the cheapest item is free (100% off). 


I was able to do this using Shopify Scripts quite easily... but the client would prefer using Shopify Functions. However, I wasn't able to get past the first step using the Product Discount API, sorting the cart line items by cost. I tried CartLine.cost but that returned undefined. Even was returning undefined, although CartLine.merchandise was returning something -- so I know I'm not totally on the wrong path. 


Does anyone know a) if this type of discount is possible, and b) why I can't even return Are the Product Discount API docs simply wrong, or out of date? 

Replies 0 (0)