How to Delete All WooCommerce Orders and Client Data on Staging Site | Delete WooCommerce Transactions

If you want to remove all WooCommerce orders and their data from your staging site before handling them to your developer, you can follow either of these two approaches:

Run an SQL Script on the Staging Site

Running this SQL script on the staging site will delete WooCommerce orders and their metadata as well

SQL
DELETE wpstg0_posts, wpstg0_postmeta, wpstg0_woocommerce_order_items, wpstg0_woocommerce_order_itemmeta
 FROM wpstg0_posts
 LEFT JOIN wpstg0_postmeta ON wpstg0_posts.ID = wpstg0_postmeta.post_id
 LEFT JOIN wpstg0_woocommerce_order_itemmeta ON wpstg0_postmeta.post_id = wpstg0_woocommerce_order_itemmeta.order_item_id
 LEFT JOIN wpstg0_woocommerce_order_items ON wpstg0_posts.ID = wpstg0_woocommerce_order_items.order_item_id
 WHERE wpstg0_posts.post_type = "shop_order"
 AND wpstg0_posts.post_date < '2022-07-14';

You need to change wpstg0_ to the correct staging site’s table prefix, and change the 2022-07-14 to the current day date, you want to delete the orders before.

You can also wrap this SQL command in a function and run it immediately after cloning the staging site using the “wpstg.clone_first_run” filter.

PHP
 add_action( 'wpstg.clone_first_run', array($this, 'wpstg_execute_after_cloning' ), 10);
 function wpstg_execute_after_cloning() {
 // add your code
 }

You can use that filter in the WP STAGING Hooks plugin or as a standard mu-plugin following this guide.

Exclude WooCommerce Orders and their Metadata When Creating the Staging Site

You can also exclude WooCommerce orders and their metadata at the beginning of the cloning process.
To do that, you can use this filter:

PHP
function queryCloningRows($filters) {
 $myFilters = [
 // Clone only posts which do not belong to post_type shop_order
 'wp_posts' => [
 'post_type' => [
 'operator' => 'NOT LIKE',
 'value' => 'shop_order'
 ]
 ],
 // will filter postmeta depending upon filtered data in wp_posts, see above wp_posts filter
 'wp_postmeta' => [
 'join' => [
 'table' => 'wp_posts',
 'primaryKey' => 'ID',
 'foreignKey' => 'post_id',
 ]
 ],
 'wp_woocommerce_order_items' => [
 'join' => [
 'table' => 'wp_posts',
 'primaryKey' => 'ID',
 'foreignKey' => 'post_id',
 ]
 ],
 'wp_woocommerce_order_itemmeta' => [
 'join' => [
 'table' => 'wp_posts',
 'primaryKey' => 'ID',
 'foreignKey' => 'post_id',
 ]
 ]
 ];
 return array_merge($filters, $myFilters);
 }
 add_filter('wpstg.cloning.database.queryRows', 'queryCloningRows');

This filter can be used in WP STAGING Hooks plugin or as a standard mu-plugin following this guide.

NOTE: Following the above approaches will delete all the orders and their relevant data from the staging site. That means you can’t push the staging site to the live site because this would also delete the WooCommerce orders from your live site. So that should be used only for testing and development purposes (if you are willing to copy the modifications manually to the live site after your developer is done).

Related Articles

Updated on May 4, 2023