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

有没有办法让WooCommerce在经过这么长时间后自动将自定义订单状态更改为其他自定义订单状态?

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

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

我只是希望能够将更改为特定状态的订单在这么多天后自动更改为其他状态.

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

最佳答案

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();

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

点赞