Count quantity of specific product per order

Topic summary

Goal: Count the quantity of a specific product in an order (by SKU/handle/tag) within Shopify Flow, then use that count to add a line item with matching quantity.

Key points:

  • The built-in “Sum of order.lineitems.quantity” aggregates all items, not a specific product.
  • One approach uses Liquid (Shopify’s templating language) in a Flow action’s Value field: iterate order.line_items, filter by product.handle (or SKU/tag), and sum li.quantity.

Proposed implementation for dynamic actions:

  • Use Flow’s “Run code” action to calculate the count. Input maps order line items (sku, quantity). The code loops line items, filters by target SKU, and totals quantity.
  • Feed the resulting count into “Send Admin API request” to add a line item to the order with that computed quantity.

Notes:

  • Shopify Flow is an automation tool; Liquid is for templating/logic in outputs; Admin API is needed to modify orders (e.g., adding line items).
  • Code snippets (Liquid and JavaScript) are central to applying the solution.

Status and open items:

  • No confirmation of execution steps or success yet. The suggested path is to compute count via “Run code” and then call the Admin API to add the line item accordingly.
Summarized with AI on December 14. AI used: gpt-5.

Hello Tim,

Thank you for the response. Im not sure if this helps clarify things but the logic Im trying to build is:

  • When an order is created :right_arrow: If the order contains certain products :right_arrow: Flow will add a line item [product] to the order (this is where the confusion starts for me) I want to add the line item quantity based on the number of products ordered

:crossed_fingers: Hope there’s a way to do this..