在特定时间过后自动更改 WooCommerce 订单状态?

Tsu*_*por 5 wordpress status orders woocommerce

有没有办法让 WooCommerce 在经过这么多时间后自动将自定义订单状态更改为不同的自定义订单状态?

基本上,我希望所有更改为订单状态“退款已提交”的订单在 30 天后自动更改为“退款已过期”。

我意识到这些不是正常的 WooCommerce 订单状态,我创建了自定义状态。因此,如果我不那么具体,可能更容易理解我想要什么......

我只是希望能够让更改为特定状态的订单在这么多天后自动更改为不同的状态。

我查看了整个互联网,并没有找到任何可以帮助我实现这一目标的东西。任何帮助将不胜感激。

muj*_*nly 1

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();
Run Code Online (Sandbox Code Playgroud)

将此代码添加到主题的functions.php中