• Resolved evalast

    (@evalast)


    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)
  • Hey there, @evalast! Thanks for contacting us.

    While we can’t provide support for code customization as per our support policy, we do our best to offer advice and direct you to appropriate resources.

    You can visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there too.

    I’m going to leave it open for a bit to see if anyone is able to chime in and help you out further.

    Please let us know if there’s anything else we can do to help or if you have any questions.

    Have a wonderful day!

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – we’ll be here if and/or when you are ready to continue.

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.