Count quantity of specific product (SKU)

On an order i want to count the ordered quantity of a specific product by SKU.

I tried doing it with Sum of order.lineitems.Quantity. however in this case, its just counting the total quantity of all products which i dont want. Is there any function that can do this ? i’ve looked around quite a bit but didn’t find anything in the app

Summing that variable would just give you the total number products ordered.

It’s not clear how you need that quantity (for a condition or in an action). But you need to check if the line item has that sku. The quantity field you found already tells you how many of that lineItem is in the order.

thanks for the input, i managed to get the correct values using custom code that looks like this :

export default function main(input) {
let Quantity = 0;
input.order.lineItems.forEach((item) => {
if (item.sku === “XX”) {
Quantity += item.quantity;
}
});
return {
Quantity: Quantity,
};
}

1 Like

Can you walk us through how can we add this using the run code feature?

Im new to this, it’ll be really helpful to see some steps.

Also do we have any alternative for this issue? in case there’s any new Flow feature that can easily integrate this.

This repo has a few run code examples, including the inputs and outputs: https://github.com/Shopify/flow-code-examples/tree/main/run-code-examples