Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically change WooCommerce order status after specific time has passed?

Is there any way to have WooCommerce Automatically change a custom order status to a different custom order status after so much time has passed?

Basically, I want all orders that are changed to Order Status "Refund-Submitted" be automatically get changed to "Refund-Expired" after 30 days.

I realize these are not normal WooCommerce Order Statuses, I created custom ones. So it might be easier to understand what I want if I'm less specific...

I just want to be able to have orders that are changed to a specific status to automatically be changed to a different status after so many days.

I looked all over the internet and have not found anything that can help me accomplish this. Any help would be greatly appreciated.

like image 406
Tsunami Premium Vapor Avatar asked Nov 08 '22 01:11

Tsunami Premium Vapor


1 Answers

function order_status_changer() {

    global $wpdb;

    $result = $wpdb->get_results("
    SELECT * 
    FROM  $wpdb->posts
        WHERE post_type = 'shop_order'
        AND post_status = 'wc-completed'
");


    $args = array(
        'date_query' => array(
            array(
                'column' => 'post_modified_gmt', // post modifed one month ago
                'after' => '1 month ago',
            ),
        ),
        'meta_query' => array(
            array(
                'key' => '_refund_expired', // skip posts that is already updated in last run
                'compare' => 'NOT EXISTS'
            )
        ),
        'posts_per_page' => -1,
        'post_type' => 'shop_order',
        'post_status' => 'refund-submitted', // slug of your status to modfiy
    );
    $query = new WP_Query($args);


    $posts = $query->posts;

    foreach ($posts as $post) {

        $ord = new WC_Order($post->ID);
        $ord->update_status('refund-expired'); // Slug to which these order to be updated
        update_post_meta($post->ID, '_refund_expired', 1); // update posts meta to note the status change for skipping on next read
    }
}
order_status_changer();

Add this code into theme's functions.php

like image 121
mujuonly Avatar answered Nov 15 '22 11:11

mujuonly