Compare order value vs that customer's previous order

Solved

Compare order value vs that customer's previous order

wilkius
Tourist
5 0 1

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!

Accepted Solution (1)

DrewOswald
Shopify Partner
10 6 4

This is an accepted solution.

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:

DrewOswald_0-1722106030528.png

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-al... 

 

The flow should look like this

img9.PNG

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)

img5.PNG

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

img7.PNG

Then an action can be performed if followUp is true.

 

I verified that this works:

img10.PNG

Need a developer? Send me a DM

View solution in original post

Replies 10 (10)

DrewOswald
Shopify Partner
10 6 4

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.

DrewOswald_0-1722038155590.png

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

img1.png

Here's a screenshot that explains the properties "Created at" and "Processed at"

img2.PNG

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

Need a developer? Send me a DM
DrewOswald
Shopify Partner
10 6 4

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" }}'"

DrewOswald_1-1722042324874.png

 

 

Need a developer? Send me a DM
wilkius
Tourist
5 0 1

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!
Screenshot 2024-07-26 at 7.58.22 PM.pngScreenshot 2024-07-26 at 7.59.12 PM.png

 

 

paul_n
Shopify Staff
1509 163 352

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.  

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.
wilkius
Tourist
5 0 1

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.

paul_n
Shopify Staff
1509 163 352

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

 

 

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.
wilkius
Tourist
5 0 1

That's great. thanks for the guidance!

DrewOswald
Shopify Partner
10 6 4

This is an accepted solution.

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:

DrewOswald_0-1722106030528.png

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-al... 

 

The flow should look like this

img9.PNG

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)

img5.PNG

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

img7.PNG

Then an action can be performed if followUp is true.

 

I verified that this works:

img10.PNG

Need a developer? Send me a DM
DrewOswald
Shopify Partner
10 6 4

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

img11.PNG

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

 

Need a developer? Send me a DM
wilkius
Tourist
5 0 1

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