Sum the value of items under specific vendor in an order.

Solved

Sum the value of items under specific vendor in an order.

Wongg
Tourist
8 0 2

E.g. I want to add a gift for orders spending a total net of $200 on XYZ products in an order.

Tried loop but only able to get the vendor and not the value.

Accepted Solution (1)

DaveMcV
Shopify Staff
104 31 29

This is an accepted solution.

Hi @Wongg,

 

You can do this using the Run code action. Here's some example code:

DaveMcV_0-1715919214524.png

 

// Inputs
query {
  order {
    lineItems {
      vendor
      originalTotalSet {
        shopMoney {
          amount
        }
      }
    }
  }
}

// outputs

"The output of Run Code"
type Output {
  "The total amount of money for the specific vendor"
  totalAmount: Float!
}


// code

export default function main(input) {
  // Make sure that the data you return matches the
  // shape & types defined in the output schema.
  const totalAmount = input.order.lineItems.reduce((total, item) => {
    if (item.vendor === "foo") {
    // Add the amount to the total if vendor is 'foo'
    return total + item.originalTotalSet.shopMoney.amount;
  }
  return total;
}, 0);

  
  return {
    totalAmount,
  };
}

 

You'll want to change the vendor name from foo to whatever your vendor is. You can then use the totalAmount variable in downstream steps.

 

Hope that helps!

DaveMcV | Flow Development 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.

View solution in original post

Replies 7 (7)

DaveMcV
Shopify Staff
104 31 29

This is an accepted solution.

Hi @Wongg,

 

You can do this using the Run code action. Here's some example code:

DaveMcV_0-1715919214524.png

 

// Inputs
query {
  order {
    lineItems {
      vendor
      originalTotalSet {
        shopMoney {
          amount
        }
      }
    }
  }
}

// outputs

"The output of Run Code"
type Output {
  "The total amount of money for the specific vendor"
  totalAmount: Float!
}


// code

export default function main(input) {
  // Make sure that the data you return matches the
  // shape & types defined in the output schema.
  const totalAmount = input.order.lineItems.reduce((total, item) => {
    if (item.vendor === "foo") {
    // Add the amount to the total if vendor is 'foo'
    return total + item.originalTotalSet.shopMoney.amount;
  }
  return total;
}, 0);

  
  return {
    totalAmount,
  };
}

 

You'll want to change the vendor name from foo to whatever your vendor is. You can then use the totalAmount variable in downstream steps.

 

Hope that helps!

DaveMcV | Flow Development 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.
Wongg
Tourist
8 0 2

Thank you! I tried using the code but it is showing the below error message.

 

Screenshot.png

DaveMcV
Shopify Staff
104 31 29

Hi @Wongg,

 

Does your workflow have a trigger that provides an order (like Order created)?  I tested it on a test shop and it worked as expected so make sure all the inputs/outputs/code are copied as is and an order is available from the trigger.

DaveMcV | Flow Development 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.
Wongg
Tourist
8 0 2

Hi @DaveMcV,

 

I updated the workflow and the code is running well now!

However I realised that it is calculating the line item original amount instead of the discounted amount.

May I know which part of the code/variable should I update? Thank you!

Wongg
Tourist
8 0 2

Hi @DaveMcV,

 

Sorry to add on, is the workflow able to retrieve and calculate the final spending after discounts are applied to the items in the order?

This is run concurrently with item auto discount and customer discount.

paul_n
Shopify Staff
1336 151 305

Those fields come from the API. I'd suggest you take a look at the API doc and see which of the fields might work for your use case. I'm pretty sure one of them will work for you: https://shopify.dev/docs/api/admin-graphql/2024-04/objects/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.
Wongg
Tourist
8 0 2

Hi @paul_n,

 

I updated "originalTotalSet" to "unfulfilledDiscountedTotalSet" and it is working ideally now, thank you!