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.

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
    };
  }
}