Skip to content
Advertisement

Remove duplicated imported orders in Woocommerce

I have duplicate orders in Woocommerce and I would like to delete them to only keep unique for clean bookkeeping.

I am not good at SQL, I wrote this request but it lists both duplicate when there is a duplicate.

JavaScript

which gives me 307 results.

How can I write the right request to delete the duplicates and only keep unique orders?

Sample data:

JavaScript

EDIT:

JavaScript

gives me 614 results, Which is the double of the previous request.

WordPress: 5.0
Woocommerce: 3.5.2

Advertisement

Answer

First if you don’t know, woocommerce Order data Is located in four (4) tables:

  • wp_posts
  • wp_postmeta
  • wp_woocommerce_order_items
  • wp_woocommerce_order_itemmeta

So the following hooked function use WPDB class and methods. It will:

  1. get all duplicated orders IDs (the lowest ID) in one query (searching for duplicated order Keys which are normally unique)
  2. delete all duplicated queried orders in one query.

But be sure to always make a database backup before.

The code will work do the job in one shot on any front end page load (to be removed after usage):

JavaScript

Code goes in function.php file of your active child theme (or active theme). Tested and work.

The duplicated orders will be removed on first frontend page load. So after that you can remove or comment the code.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement