How to automatically cancel duplicate orders with same SKU within 24 hours?

How to automatically cancel duplicate orders with same SKU within 24 hours?

Chegism
Tourist
7 0 1

Hi,

We're battling customers trying to get around our sale limits of 1 per SKU by placing multiple orders.

 

I'm trying to use Flow to automatically cancel an order if they place a second order within 24 hours containing the same SKU.

i.e.
Order 1 SKU ABC x1 goes through
Order 2 SKU BDC x1 goes through
Order 3 SKU ABC x1 Auto-Canceled

I'm able to get a count of the customers orders placed within the last 24 hours. I can even action that based on how many orders they have placed.
But when I try to filter it down to SKU all I'm getting is a count of ALL SKUs across all orders in that 24 hour time period. Not just SKU ABC.

This was getting very close: community.shopify.com/c/shopify-flow-app/checking-skus-in-a-customers-previous-orders/td-p/2328679, but the person stopped replying before @paul_n was able to finish helping out.

Any ideas the next step in the flow to count only the line items that match SKU ABC?

Replies 14 (14)

deepaksharma
Shopify Partner
449 63 99

Hello @Chegism 

I am assuming you want your customer to buy 1 unique SKU per order, but they can order multiple different SKU per order, am I right?

Deepak Sharma || Shopify Developer || Helping eCommerce Stores
- Was my reply helpful? Accept it as solution
- Was your question answered? Mark it as an Accepted Solution.
-CHAT ON WHATSAPP | BUY ME A COFFEE | MAIL ME: [email protected]
Chegism
Tourist
7 0 1

Correct. They can also place multiple different orders - but we want to auto-cancel if the order the same SKU a second time.

paul_n
Shopify Staff
1336 151 305

Where are you trying to do that count? I'm assuming your workflow looks like:

  • Order created
  • Get order data (customer:{{order.customer.legacyResourceId}} sku:the_sku)
  • Count (Get order data)
  • If count > 1
    • Cancel current 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.
Chegism
Tourist
7 0 1

Order Created

Get Order Data: created_at:>'{{ "now" | date_minus: "24 hours" }}' AND customer_id {{order.customer.legacyResourceID}} AND sku: ABC

 

But it isn't counting the SKU I'm adding at all, it's being ignored (i.e. placing a new order for a completely different SKU is still triggering it)

paul_n
Shopify Staff
1336 151 305

Are you using lowercase sku? It works ok in my store: 

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

No, I was using uppercase as that's what was listed for the SKU back on the product page.


Does only lowercase work, and does it need to be wrapped in ""?

paul_n
Shopify Staff
1336 151 305

It is usually safe to wrap and helpful when it can have spaces.

 

The doc for what query variables are available is here:

https://shopify.dev/docs/api/admin-graphql/2024-01/objects/QueryRoot#connection-orders (you'll have to expand it a couple of times)

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.
paul_n
Shopify Staff
1336 151 305

FYI, you also appear to be missing a colon and using the wrong variable for customer (customer_id and ResourceID both wrong). Try:

 

customer:{{order.customer.legacyResourceId}}
Full query:
created_at:>'{{ "now" | date_minus: "24 hours" }}' AND customer:{{order.customer.legacyResourceId}} AND sku:ABC

 

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

I have this now.

 

Order Created

Get Order Data (this code, changing customer_id to customer)

Count items in getOrderData

Check if Count is greater than or equal to 2

 

It's firing on every single order that comes in, even customers with 1 order for a different SKU.

paul_n
Shopify Staff
1336 151 305

By firing, I'm assuming you mean that runs for every order, which is how Order created works.

 

If you want that logic to only execute for when the SKU is present in the current order, add a condition before the "Get order data" to check the current sku. (if at least one of order/lineItems/sku == YOURSKU)
 

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

By firing I actually meant the end result of canceling the order. In my testing I have it set to send an internal email instead as that doesn't affect the customers.

  • Order Created
  • Get Order Data (created_at:>'{{ "now" | date_minus: "24 hours" }}' AND customer:{{order.customer.legacyResourceId}} AND sku:ABC)
  • Count items in getOrderData
  • Check if Count is greater than or equal to 2
  • Send Internal Email


Expected: If a customer places 1 order for SKU ABC, nothing happens. If a customer places 1 order for SKU EFG, nothing happens. If a customer places a second order for SKU ABC, send internal email.

What is happening: If a customer places 1 order for SKU ABC, send internal email.. If a customer places 1 order for SKU EFG, send internal email. If a customer places a second order for SKU ABC, send internal email.

I have added your suggestion to do a Check if SKU includes ABC before Get Order Data, which has worked to remove someone purchasing a completely different SKU from the flow. But the check if count of 2+ isn't working and it's still sending that email on the very first order.

paul_n
Shopify Staff
1336 151 305

I think I need to see an image of your workflow because something isn't set up correctly

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

Screenshot 2024-03-06 102811.png

paul_n
Shopify Staff
1336 151 305

Looks right. You might need to share the link of the workflow so we can look closer. You can either go through support to share that or DM me here. 

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.