Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
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:
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!
Solved! Go to the solution
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:
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
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:
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!
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:
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
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.
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024