Woocommerce-delete-all-orders icon indicating copy to clipboard operation
Woocommerce-delete-all-orders copied to clipboard

other solution

Open sakp opened this issue 6 years ago • 5 comments

update wp_posts set post_status = 'trash' where post_type = 'shop_order'

login to admin -> order / trash -> empty trash

sakp avatar Jun 06 '19 17:06 sakp

This works, but I like the safety of using WooCommerce's PHP objects for this kind of manipulation. Here's what I came up with:

    public function delete_all_orders(){
        $orders = wc_get_orders(array('limit' => -1)); // sort of bizarre this one requires pagination, and none of the others do...? 
        printf("Length of orders = %d", sizeof($orders));
        foreach ($orders as &$order){
            wc_delete_order_item_meta($order->get_id(), null, null, true); // last parameter = delete_all
            $order->delete(1); // set to 1 if you want these actually deleted, otherwise they'll be moved to trash
        }
    }

Don't forget to delete anything from _postmeta that has a post_id you just deleted, otherwise you'll have orphaned meta-data lying around.

I've got a bunch of PHP routines for this type of order/user/order-line-item manipulation if you're interested in this approach. Like this, if you're super set on MySQL:

DELETE FROM wp_8b772b21c3_postmeta WHERE post_id IN (SELECT ID FROM wp_8b772b21c3_posts WHERE post_type = 'shop_order'); 

brian-stinar avatar Jun 06 '19 22:06 brian-stinar

@sakp Sounds like something we should add to the README. I think it's great to have alternatives. Will you make a pull-request or do you want me to do it?

@brian-stinar How do you execute this?

mauran avatar Jun 07 '19 06:06 mauran

@mauran

if (php_sapi_name() == "cli"){
    include_once("cli_credentials.php");

    $annieMigrator = new AnnieMigrator($old_database_host, $old_database_name, $old_database_user, $old_database_password, $old_database_port);
    $annieMigrator->delete_all_orders(); 
    $annieMigrator->load_orders(10);
}

then from bash: $ php noventum-migratation-object.php I also built a plugin that operated on a button click, and called it on POST of the admin form. That's why I had the if (php_sapi_name() == "cli"){. It can run either from the command line, or from the plugin. That's like this:

function noventum_data_migrator_action() {
    add_options_page('Noventum Data Migrator', 'Noventum Data Migrator', 1, 'noventum_data_migrator_config', 'noventum_data_migrator_config');
}

function noventum_data_migrator_config(){
    if ( !current_user_can( 'manage_options' ) )  {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }

    if($_POST['submitted']) {
.... do a bunch of checking to see what to call ... 
... and here's how to add the admin action to make this available within the admin menu ... 
if (php_sapi_name() != "cli") {
    add_action('admin_menu', 'noventum_data_migrator_action');
}

This mechanism allows for developers to quickly call this from the command line, AND a nice admin-menu inside the WordPress dashboard. I recommend this approach above the direct database access method, since it allows for using the WordPress API to access this, which is (hopefully) more stable than direct database access.

Add me on LinkedIn if you want. You seem cool. Thanks for discussing this publicly, it helped me formulate some ideas for my project.

brian-stinar avatar Jun 13 '19 18:06 brian-stinar

@brian-stinar sounds pretty cool! If you ever open source it, i would gladly link to your repository/project from this repo. I agree on that your approach is way more safe than just doing this directly from the DB. I rarely touch Wordpress, so doing these SQL queries was the easiest way i knew, when making this repository 😄

I added you on LinkedIn btw :)

mauran avatar Jun 13 '19 19:06 mauran

@mauran I've got a bunch of customer specific code in here too. If the project still has any $$$$ I'll try and create a base object that does all of this general stuff, and a derived object that implements the customer specific code. It would make sense to open source the base. We'll see, as always, dealing with resources is much more of a constraint than anything technical... If that happens, I'll shoot you a message.

Thanks for the adds. I'd like to hear about Zitcom A/S, and Promotr, and what being a developer in Denmark is like. I'm sure it's VASTLY different than owning a software company in the United States, even if we have similar (technical) problems.

Don't forget to clean up the post meta though, otherwise you'll be leaving orphans around.

DELETE FROM wp_8b772b21c3_postmeta WHERE post_id IN (SELECT ID FROM wp_8b772b21c3_posts WHERE post_type = 'shop_order');

brian-stinar avatar Jun 13 '19 19:06 brian-stinar