WordPress order remove after 1 month or 30 days
if you want to change status of order in woo commerce then it will help for you, it will also help you to delete same staus order after 30 days with ( wp_delete_post( $orders[‘ID’]); ) fuciton.
please add this function in your child theme function.php and it will work fine.
function expire_after_30_days(){
global $wpdb;
// Get current time
$today = date(“mdy”);
// set time to expire
$time_to_expire = “-30 days”;
$expiration_date = date(“mdy”, strtotime( $today . $time_to_expire));
// Get orders with processing status
$result = $wpdb->get_results(“SELECT * FROM $wpdb->posts WHERE post_type = ‘shop_order’ AND post_status = ‘wc-processing'”);
if( !empty($result)) foreach ($result as $order){
// Get order’s time
$order_time = get_the_time(‘mdy’, $order->ID );
// Compare order’s time with current time -30 days
if ( $order_time < $expiration_date ){
// Update order status
$orders = array();
$orders[‘ID’] = $order->ID;
$orders[‘post_status’] = ‘wc-cancelled’;
wp_update_post( $orders );
// wp_delete_post( $orders[‘ID’]);
}
}
}
// Use the best HOOK for your case
add_action( ‘admin_footer’, ‘expire_after_30_days’ );