HPOS – Automatically delete completed orders in Woocommerce older then 3 months
-
I need to set up Woocommerce to automatically clean up completed orders older then 3 months and found?this solution?on stackoverflow which does not work with HPOS activated. I added some debug statements and getting?
Error deleting order ID:
?for each Order ID. Any idea how to fix it?add_action( 'admin_footer', 'auto_trash_old_completed_orders_345erhwe546' ); function auto_trash_old_completed_orders_345erhwe546() { global $nm_theme_options; if ( isset( $nm_theme_options['delete_all_woo_orders_older_then_3_months'] ) && $nm_theme_options['delete_all_woo_orders_older_then_3_months'] == 1 ) { // Theme option is set to delete old orders $completed_order_ids = wc_get_orders( array( 'limit' => 100, // By batch of 100 orders 'status' => 'completed', 'return' => 'ids', 'date_created' => '<' . date( 'Y-m-d', strtotime( '-3 months' ) ), ) ); if ( count( $completed_order_ids ) > 0 ) { // Orders found, proceed to delete foreach ( $completed_order_ids as $completed_order_id ) { $trashed = wp_trash_post( $completed_order_id ); $deleted = wp_delete_post( $completed_order_id, true ); // DEBUG if ( ! $trashed || ! $deleted ) { echo '<script>alert("Error deleting order ID: ' . $completed_order_id . '");</script>'; } } } // DEBUG echo '<script>alert("Orders deleted: ' . count( $completed_order_ids ) . '");</script>'; } else { // DEBUG echo '<script>alert("Theme option not set to delete old orders.");</script>'; } }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘HPOS – Automatically delete completed orders in Woocommerce older then 3 months’ is closed to new replies.