Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Count quantity of specific product per order

Count quantity of specific product per order

JS_Builder
Shopify Partner
3 0 0

 

When an order is created, I want to count the ordered quantity of a specific product (can be by SKU or tags etc)

 

I tried doing it with Sum of order.lineitems.Quantity. However, its just counting the total quantity of all products in an order which I dont want.

 

Is there any function that can do this? like either using tag, URL or anything that will help me implement this easily

 

I've looked around quite a bit but didn't find anything in the app. Please help. Newbie here - may need some steps. 

Replies 3 (3)

tim
Shopify Partner
3911 394 1435

I believe you can't do this directly with Flow builder.

You'd need to use Liquid to do it.

Not sure what Action you want to perform with this count, but in the "Value" entry field you can enter Liquid similar to this:

 

{%- liquid
  assign count = 0
  for li in order.line_items
    if li.product.handle == "handle-of-special-product"
      assign count = count | plus: li.quantity
    endif
  endfor
-%}
In this order there is {{ count }} of our special product. 

 

 

 

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
JS_Builder
Shopify Partner
3 0 0

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  ➡️ If the order contains certain products ➡️  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

 

🤞Hope there's a way to do this..

Yuka
Shopify Partner
6 1 0

You can use “Run code” action to calculate the number of items in an order:
Input:

 

query{
  order{
    lineItems{
      sku
      quantity
    }
  }
}

 

 Output:

 

type Output {
  "The total number of items returned by this buyer"
  totalQuantyty: Int!
}

 

 Code:

 

export default function main(input) {
  let totalQuantyty = 0;
  input.order.lineItems.forEach(lineItem => {
    if (lineItem.sku === 'SOME_SKU') {
      totalQuantyty += lineItem.quantity;
    }
  });
  
  return {
    totalQuantyty,
  }
}

 

Then, you can use the resulting value in the “Send Admin API request” action.