We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Count quantity of specific product (SKU)

Solved

Count quantity of specific product (SKU)

Cleronhigh
Shopify Partner
6 1 1

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

Accepted Solution (1)
Cleronhigh
Shopify Partner
6 1 1

This is an accepted solution.

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,
};
}

View solution in original post

Replies 4 (4)

paul_n
Shopify Staff
1828 199 435

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. 

 

 

 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
Cleronhigh
Shopify Partner
6 1 1

This is an accepted solution.

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,
};
}

JS_Builder
Shopify Partner
3 0 0

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.

 

paul_n
Shopify Staff
1828 199 435

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

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.