Draft_order_creation webhook used to add a custom OrderID.... until it didn't

Solved

Draft_order_creation webhook used to add a custom OrderID.... until it didn't

gagegreg
Shopify Partner
3 1 0

I made a custom app under settings -> apps -> develop app and have used it to connect to 2 webhooks: draft_order_create and order_create. 

I set up the webhooks to retrieve the draft_order, or order and attach a note in our format.  BYB-ORD-2411-0023412.  (2411 = YYMM, Nov 2024).  It works, and I can see that the API server is handling everything well. 

INFO:api.main:Custom ID and attributes set for ORD 6069430681841

INFO:api.main:Webhook processed in 0.442 seconds for order 6069430681841

NFO:httpx:HTTP Request: PUT https://xxx.myshopify.com/admin/api/2023-10/draft_orders/11

I was able to use this to replace the ordered with {{ draft_order.note }}.  In the admin panel, I would create a draft order, add an item, and when I went to send it.... I would see the API server clicking away,  and when I would send an email... it would show up with the correct order IDs.  Invoice BYB-ORD-2411-0023412.  I thought I was a genius.  

I had to rebuild the server today, and so I re-added my API server from github.  It is the same code.   However, now when it runs, there is no notes on the order when the email pops up.  If I refresh the screen, I can see the note has been added... but the email will ignore the note.  I suspect it could be timing.  Maybe I got lucky last week, and today the internet is a little slower.

I cannot figure out how to make this synchronous, so the next step won't start before I finish modifying the order from my API.  (I have tried attributes, tags as well... but all appear as blank). 

What is the proper way to do this?  I suspect its not with webhooks as these are asynchronous.   It is strange that even after the note is there... the email ignores it.    


Here is proof that the webhook APIs are working:
Screenshot 2024-12-05 at 18.40.16.png


This is the draft_order template

Screenshot 2024-12-05 at 18.40.53.png

And this is the dam subject!  No notes displayed.
Screenshot 2024-12-05 at 18.41.32.png

Accepted Solution (1)

gagegreg
Shopify Partner
3 1 0

This is an accepted solution.

OK.  So I figured out a workaround.  There is beauty in the creativity inspired by Shopify's limitations.

The issue, as clockfaces nicely pointed out, was a race between the API and Shopify Order Processing.  The winner was intermittent, and I could never resolve it 100%. I ended up solving this by ignoring the race complely...  I am still processing the webhook, but am now calculating our fancy order# twice... including in the email driectly using Loquid.

Our order name now includes the month and year.  here is the code in the email template editor to make the subject (in Dec 2024) read 
Order # SPIKE-2412-009999.

Order # {% assign year_month = order.created_at | date: "%y%m" %} {% assign order_str = order.name | remove: "#" | plus: 0 %} {% assign padded_order_number = order_str | plus: 1000000 | slice: 1,6 %} SPIKE-{{ year_month }}-{{ padded_order_number }}


Perhaps someone in the future may want to use this to make a fancier custom order name without paying a monthly subscription to fix something simple. 

View solution in original post

Replies 3 (3)

clockfaces
Tourist
5 0 2

It sounds like the issue is related to the asynchronous nature of Shopify webhooks. To make sure the note is included when the email is sent, you might want to ensure that the note update is fully completed before the order email is triggered.

A simple solution could be using a "delayed" approach, where you delay sending the email by a few seconds after the note is added. You can implement this delay within your API call or by using a queuing system to ensure the note is properly synced before triggering the email.

Alternatively, you could try using the Order API (after the draft is created) to ensure the note is properly updated. Testing with a small delay can help confirm if it's timing-related.

gagegreg
Shopify Partner
3 1 0

 

Thanks for your kind response and insights. I’ve spent the past several hours digging into this issue and, so far, haven’t found a way to alter the timing of Shopify’s built-in order confirmation emails. From what I can tell, the confirmation email is triggered almost immediately when an order is created, leaving little room for updates like adding a note before the email is sent.

 

Unfortunately, I don’t have Shopify Plus, so options like custom checkout workflows or Shopify Flow are moot for me. I’m now wondering if there’s any way—using a standard Shopify setup—to introduce a delay or otherwise ensure that my updates (e.g., notes) are applied before the email goes out.

 

Has anyone managed to solve this using webhooks or other custom app-based methods? Are there any reliable workarounds I might be missing? I see there are 3rd party apps that do this. I wonder how they did it.  I’d appreciate any guidance or creative ideas!  

gagegreg
Shopify Partner
3 1 0

This is an accepted solution.

OK.  So I figured out a workaround.  There is beauty in the creativity inspired by Shopify's limitations.

The issue, as clockfaces nicely pointed out, was a race between the API and Shopify Order Processing.  The winner was intermittent, and I could never resolve it 100%. I ended up solving this by ignoring the race complely...  I am still processing the webhook, but am now calculating our fancy order# twice... including in the email driectly using Loquid.

Our order name now includes the month and year.  here is the code in the email template editor to make the subject (in Dec 2024) read 
Order # SPIKE-2412-009999.

Order # {% assign year_month = order.created_at | date: "%y%m" %} {% assign order_str = order.name | remove: "#" | plus: 0 %} {% assign padded_order_number = order_str | plus: 1000000 | slice: 1,6 %} SPIKE-{{ year_month }}-{{ padded_order_number }}


Perhaps someone in the future may want to use this to make a fancier custom order name without paying a monthly subscription to fix something simple.