Turn Off Pesky Order Notes for Woocommerce Order/Customer CSV Export

Cash RegisterOne of my clients uses the plugin Woocommerce Order/Customer CSV Export to export his customers’ orders into a spreadsheet for managing a mailing list. The other day, he emailed me to tell me that instead of being prompted to download the CSV when he clicked the “Export” button, he was getting a nasty screen-full of text.

It took a while to troubleshoot, but it turned out the problem was caused by the 34,000+ records that CSV Export had inserted into the WordPress comments and commentmeta tables. Each time the client was clicking that “Export” button, a new record was added to each table for each exported order, and as the number of orders added up, the records were increasing proportionally. Not only was it causing serious database-bloat, but it was also cluttering up the Order Notes box for each order with “Order exported to CSV and successfully downloaded” notes that the client neither wanted or needed.

The problem would most likely never have occurred if the client was using the plugin’s incremental automatic exports, in which case there would have been only a single “Order exported to CSV and successfully downloaded” added to the Order Notes box for each order. However, he was doing all his exports manually, and grabbing all the orders every time. It was only a matter of time before all those Order Notes records would cause a problem.

As of this date, there was nothing in Woothemes’ documentation for CSV Export about turning off the order notes feature, and there was no setting in CSV Export’s Settings page to disable it.

It took a bit of combing through the code for CSV Export, but I eventually found the following code on line 347 of the file includes/class-wc-customer-order-csv-export-handler.php:

if ( apply_filters( 'wc_customer_order_csv_export_add_order_note', true ) ) {
  $order->add_order_note( sprintf( __( 'Order exported to CSV and successfully %s', WC_Customer_Order_CSV_Export::TEXT_DOMAIN ), $message ) );
}

Thank goodness for developers who think ahead! The developer of CSV Export had thoughtfully included that bit of code to make it possible to disable the order notes.

The Fix

All that was needed to turn off the unwanted “Order exported to CSV…” additions to Order Notes was to add the following code to the functions.php of the child theme on my client’s site:

// Disable adding an order note every time an export is done.
add_filter( 'wc_customer_order_csv_export_add_order_note', 'disable_add_order_note');

function disable_add_order_note() {
    return false;
}

With the addition of that filter, my client could manually export customer orders without generating all those superfluous (for him) data records. It will be years before the number of orders he has will cause a similar issue with the size of the export.

The Leftovers

What about those 34,000+ extra records in the comments and commentmeta tables? There was no way I was going to go through all the orders and manually delete all those extraneous entries in Order Notes. I don’t like to work directly on the data tables for WordPress if I can avoid it, but in this case, I couldn’t find a reasonable alternative. I used phpMyAdmin to run a couple of simple queries to clean out all the unwanted records. I’ll share them here, but if you use them, you do so at your own risk. I haven’t noticed any issues after doing the deletions, but that isn’t to say you won’t. Make sure to back up your database first.

The first query cleans out the commentmeta table:

DELETE FROM commentmeta WHERE comment_id IN(SELECT comment_ID FROM comments WHERE comment_content = 'Order exported to CSV and successfully downloaded.')

The second query cleans out the comments table:

DELETE FROM comments WHERE comment_content = 'Order exported to CSV and successfully downloaded.'

After running the queries, all the unwanted entries in Order Notes were gone, and all the orders could be exported successfully without generating new ones.

I submitted a support ticket to Woothemes asking that they add documentation about the order notes filter and/or a setting on the CSV Export Settings page to make disabling the order notes feature easier, but until they do, send the developers of CSV Export a silent “thank you” for including the filter for us to use in the meantime.