Compare order value vs that customer's previous order

Topic summary

A merchant wants to create a Shopify Flow workflow that triggers when a customer places their 2nd or 3rd order, comparing the order value against their previous order. The goal is to identify when a second order falls below a threshold (e.g., $728) after a first order exceeded it, enabling targeted follow-up promotions.

Key Challenge:
The user struggled with using the “Get order data” action to retrieve and compare the last two orders’ subtotals.

Solution Provided:

  • Sort orders by “Created at” in descending order to get the most recent orders first
  • Use query filter “Placed by same customer in last day” (removing the date constraint)
  • Since Flow conditions cannot directly compare two list items, use the “Run code” action
  • The code checks if customer has 2+ orders, parses subtotals of current and previous orders, then returns a boolean “followUp” value
  • A subsequent condition step checks if followUp is true to trigger actions

Important Note:
The Customer.numberOfOrders property has a 1-2 hour delay in updating, so that check was removed from the workflow.

Outcome:
Complete working code examples were provided for both scenarios: any order after the first, and specifically for 2nd/3rd orders only.

Summarized with AI on November 7. AI used: claude-sonnet-4-5-20250929.

I want to trigger a workflow when a customer makes an order and it’s their 2nd (or 3rd) order.

Specifically I am trying to figure out if the first order is above $X amount and the second order is below $X amount so that I can then respond to that customer to try and increase their next sale / offer a promo, etc.

Shopify help suggested using the following:

  • Use the Order fulfilled trigger to start your workflow when an order is fulfilled.
  • Add a condition to check if the customer has made at least two orders.
  • Retrieve the details of the last two orders using the Get order data action.
  • Compare the subtotal of the second order (the one being fulfilled) with your specified amount (below $X) and the subtotal of the first order (above $X).

Where I am stuck is in using the “get order data” action. No idea how to make this comparison. Any help is appreciated. When I asked Shopify help to throw that example in my workflow or show me, they took 20 minutes to then tell me to use a template instead (which doesn’t do what I’m requesting).

Thanks folks!

Hi Wilkius,

To get the last two orders, you should use Sort data by “Created at” in Descending order. Sorting in descending order ensures that the most recent orders are at the top of the list. Once sorted, you can then retrieve the first two orders from this sorted list.

Here is an example of orders being sorted by “Created at” in Descending order and Ascending order in the Shopify dashboard

Here’s a screenshot that explains the properties “Created at” and “Processed at”

Reference: https://shopify.dev/docs/api/admin-rest/2024-07/resources/order#resource-object

One more thing, for “Select a query to filter data” choose “Placed by same customer in last day” and then remove " AND created_at:>=‘{{ “now” | date_minus:“1 day” }}’"

Thanks a ton for this, Drew.

I guess my next question is how to I then do the comparison from order 1 to order 2 after completing the query?

What I’ve tried here is adding the condition that one of the orders from the list that is generated is >$728 (my threshold number). I think… Is this accurate or is there a more elegant / foolproof way to get to this answer? THANKS!

You cannot compare 2 separate list items in a condition step. You can use the “Run code” action, either by doing the comparison there, or by splitting the 2 orders and returning the data you need for a subsequent condition.

Thanks Paul.
Any resources on what that code might look like? I run a store for a living, not code. I have no idea how to code a comparison or split the orders and return the data I need for a subsequent condition.

We have examples in repo here: https://github.com/Shopify/flow-code-examples/tree/main/run-code-examples

That’s great. thanks for the guidance!

Hi Wilkius,

Sorry for the delay. I was testing the flow and ran into a problem with checking how many orders a customer has in this step:

It was always showing 0 even though I had at least 4 orders. Apparently it takes 1-2hr for Custom.numberOfOrders to update when orders are created: https://community.shopify.com/c/hydrogen-headless-and-storefront/grapshql-customer-numberoforders-always-quot-0-quot/m-p/2217109

The flow should look like this

Notice: I removed the step “Check if… Customer number of orders is equal to 2”

In Run Code:

We check if the customers orders are less than 2 here and only proceed with the comparison if the customer has 2 or more orders (for exactly the 2nd and 3rd order see reply)

This returns followUp which can be true or false

Code to Copy & Paste (check if the conditional logic is correct first)

Define Inputs

{
  getOrderData {
    currentSubtotalPriceSet {
      shopMoney {
        amount
      }
    }
  }
}

Define ouputs

"The output of Run Code"
type Output {
  followUp: Boolean!
}

Write code

export default function main(input) {
  const orders = input.getOrderData;

  // Check if there are fewer than 2 orders
  if (orders.length < 2) {
    console.log(`Number of orders: ${orders.length}`);
    return {
      followUp: false
    };
  }

  // Parse the current and previous orders' subtotal amounts as floats
  const currentOrderAmount = parseFloat(orders[0].currentSubtotalPriceSet.shopMoney.amount);
  const previousOrderAmount = parseFloat(orders[1].currentSubtotalPriceSet.shopMoney.amount);
  console.log(orders);

  // Check the conditions and return the appropriate object
  const followUp = currentOrderAmount < 728 && previousOrderAmount > 728;

  return {
    followUp
  };
}

The we do a condition step to check if followUp is true

Then an action can be performed if followUp is true.

I verified that this works:

To have this apply only to the 2nd and 3rd orders apply these changes:

Here we proceed with the comparison only if the number of orders is 2 or 3 otherwise we return followUp is false

Write Code

export default function main(input) {
  const orders = input.getOrderData;

  // Check if the number of orders is 2 or 3
  if (orders.length === 2 || orders.length === 3) {
    // Parse the current and previous orders' subtotal amounts as floats
    const currentOrderAmount = parseFloat(orders[0].currentSubtotalPriceSet.shopMoney.amount);
    const previousOrderAmount = parseFloat(orders[1].currentSubtotalPriceSet.shopMoney.amount);
    console.log(orders);

    // Check the conditions and return the appropriate object
    const followUp = currentOrderAmount < 728 && previousOrderAmount > 728;
    
    return {
      followUp
    };
  } else {
    return {
      followUp: false
    };
  }
}

Incredible. Thank you SO much for that effort. I truly appreciate it.

1 Like