Restoring an order in WooCommerce
I recently had to revert a client’s database to a backup from a couple of days earlier. Some new changes had had unforseen consequences, which had not been discovered in testing.
Doing so, meant we lost an order that had just come in. I had of course taken an export of the running database, before reversing it, so I loaded it up in my local phpmyadmin and started pulling out data. Here’s what one needs to restore a woocommerce order
1. Finding the order itself
In the posts table, search for post_type = “shop_order” and order by ID or date to get the latest ones. Find your order and export it. Note the ID.
2. Order metadata
In the postmeta table, search for all records with post_id matching your order. Export it.
3. Order comments
I’m not sure it works similiarly across all implementations, but for us, woocommerce saves all payment events as comments to the order (post).
So in the “comments” table, search for “comment_post_ID” matching your order. Export it.
4. Order items
The items belonging to the order. This is the table “woocommerce_order_items”. Match “order_id” with your orders ID.
Aaaand export it.
There will be several rows here. One for each item in the order and one for VAT. You need to note the ID’s of these rows for the next step.
5. Order item metadata
Last piece of the puzzle: In the “woocommerce_order_itemmeta” table, match the column “order_item_id” with the ID’s from step 4. So something like:
“SELECT * FROM woocommerce_order_itemmeta WHERE order_item_id IN ([order item row id’s seperated by comma])”
And export them. Finalizing
This should give you all the missing data to import into your current database.