Set Order Metadata Field with total of all line items that have a product with a specific tag.

Set Order Metadata Field with total of all line items that have a product with a specific tag.

AvyDan
Tourist
4 1 3

Trying to create a flow that will add a total the lineitem price in an order for all lineItems that have a product with a specific Tag.

 

In particular, we have a few different items in our store that are donations (membership, specific campaigns, and just donating).  I need to total all the line items that are of type donation, so that we can send an email for tax deduction purposes.

 

I thought I was onto something with the flow attached, but for the value, I couldn't figure out how to reference the line item the flow is iterating through.

AvyDan_0-1726789212409.png

 

Replies 2 (2)

Kalen_Jordan
Shopify Partner
727 34 127

You're going to probably need a little run code step for this.

AvyDan
Tourist
4 1 3

Thanks Kalen, you were correct.  The flow I was using didn't seem to return the Sum to the proper place in the flow.

In case future people need the solution, I was able to get it to work with the following:

AvyDan_0-1726955825418.png

Run Code

  • Inputs

 

query{
  order{
    id,
    lineItems {
      product {
        tags
      },
        discountedTotalSet{
          shopMoney {
            amount
          }
        }
    }
  }
}​

 

  • Output

 

type Output {
totalDonation: Float!
}​

 

  • Code

 

export default function main(input) {
  const tagToMatch = "Donation";
  const totalDonation = input.order.lineItems.reduce((total, item) => {
    if (item.product.tags.includes(tagToMatch)) {
    return total + item.discountedTotalSet.shopMoney.amount;
  }
  return total;
}, 0);

  return {
    totalDonation
  }
}​