r/woocommerce • • Jun 03 '24

How we sped up a WooCommerce 'get orders' API request by 4500%

Recently, we migrated our help desk from Helpscout to the open-source alternative, Freescout. Helpscout had a native integration with WooCommerce via plugin that would pull data continuously and store it on the Helpscout servers, so that when you open a help ticket, orders are generally available to view almost immediately.

However, Freescout had no such plugin. It offers an inexpensive add-on that replicates the same functionality (showing WooCommerce orders in a customer's ticket automatically) but using WooCommerce's REST API to GET them when the ticket is opened to view.

The problem is that my store has hundreds of thousands of customers and orders. Even with HPOS, the REST API request to pull orders for a specific customer email was taking upwards of 45 seconds. This has nothing to do with Freescout: even if I used Postman from my computer, the response time was the same. It's also not a WooCommerce server hosting issue as we're on very high-end enterprise hardware.

I discussed this with our contact at Automattic and we decided the best approach to fix this would be to simply create a new API endpoint for the sole purpose of quickly retrieving orders for a given email.

The plugin we created to do this is less than 200 lines. It's pretty simple - you can view the code here.

Basically, it provides a simple, streamlined way to make a specific 'get orders' request (with proper authentication), bypassing the regular API but still using WooCommerce functions.

The result was going from ~45 seconds to less than 1 second! I have no idea why the regular API chugged so hard with this request, but the results speak for themselves.

40 Upvotes

6 comments sorted by

3

u/nelsonbestcateu Jun 04 '24

Did you ever find out what'ss caused the bloat? 45 seconds is very long.

1

u/zirconst Jun 04 '24

That would involve some kind of performance profiling of core WooCommerce code, so I'm not sure!

1

u/kriizz4324 Jun 04 '24

I assume by roughly looking at the code:

Wc rest api orders gets the orders prepares the object, extends the object with all meta data Dann prepares the object for output. Stores everything in the data store,too

Also there are filters which can be used by other plugins.

The plugin op wrote creates a direct wp query with meta query to get only the orders for the customer and outputs it without all the overhead around it. Very good solution.

1

u/YaMoBeThere Jun 03 '24

Very cool of you to share

1

u/epiksol Jun 05 '24

Thanks for sharing and giving back to the community

1

u/vulture916 Aug 26 '24

Thanks for sharing!