Exporting/Importing Woocommerce Orders with SQL 2020

Covers WordPress version 5.3.2

No matter how you use the SQL, be it on the command line, PhpMyAdmin or PHP, These are just the SQL commands needed.

There are 4 tables invlovled not counting the variable product data. I’ll be adding that as soon as a get a free moment.

  • wp_posts
  • wp_postmeta
  • wp_woocommerce_order_items
  • wp_woocommerce_order_itemmeta

wp_posts are where the main orders are kepts, with the post_id being the actual order ID. The other 3 tables use this id to attach items, customer details, etc to the order.

wp_postmeta contains the customer data.

wp_woocommerce_order_items contains the items that are ordered.

wp_woocommerce_order_itemmeta contains the price, etc of the items ordered.

So, on to the SQL by example

SELECT * FROM wp_posts WHERE post_type="shop_order"  and date(post_date)=date("2020-02-25")

The main thing is the “shop_order” post type. If you leve out the date part of the SQL then obviously you will get all orders in the database. If using PhpMyAdmin you can then simply export the results, os CLI pipe to a file, whatever, saving the data into something like wp_posts.sql

Now assuming a list of orders with post_id’s of 11538, 11541,11542, 11543, 11533, 11534, 11535, 11536, 11537, 11539, 11540 was resturned. We need to get all the info for those numbers starting with wp_postmeta.

SELECT * FROM wp_posts WHERE post_type="shop_order"  and date(post_date)=date("2020-02-25")

Save the results to wp_postmeta.sql.

SELECT * FROM wp_woocommerce_order_items WHERE order_id in (11538,11541,11542,11543,11533,11534,11535,11536,11537,11539,11540)

This will give us the items ordered, so save teh results to wp_woocommerce_order_items.sql.

SELECT * FROM wp_woocommerce_order_itemmeta WHERE order_item_id in (11538,11541,11542,11543,11533,11534,11535,11536,11537,11539,11540)

And save these results to wp_woocommerce_order_itemmeta.sql.

So now we have 4 sql files containing the orders and order data that we want to import, possibly on another server. The assumption at this point is the users exist already, otherwise you will have to export the users as well which is beyond the scope of this blog.

If would/could be a very, very bad idea so simply import as it, as chances are the key values are already in use.

The plan therefore is simple, edit the .sql files and change them to a number higher that what exists in the database you are importing to.

So you would change the post_id in the first file, then edit the second file to point to the new numbers you created, as well as create new meta_id values, and so forth. All the id numbers in the 4 files must match up.

Then you can simply import the 4 files into the new database, and there you go, your orders have been imported.

Summary

SELECT * FROM wp_posts WHERE post_type="shop_order"  and date(post_date)=date("2020-02-25")

SELECT * FROM wp_postmeta WHERE post_id in (11538,11541,11542,11543,11533,11534,11535,11536,11537,11539,11540)

SELECT * FROM wp_woocommerce_order_items WHERE order_id in (11538,11541,11542,11543,11533,11534,11535,11536,11537,11539,11540,11545)

SELECT * FROM wp_woocommerce_order_itemmeta WHERE order_item_id in (11538,11541,11542,11543,11533,11534,11535,11536,11537,11539,11540,11545)

Thank you for your time, I hope this helps.