在特定时间过后自动更改 WooCommerce 订单状态? [英] Automatically change WooCommerce order status after specific time has passed?

查看:55
本文介绍了在特定时间过后自动更改 WooCommerce 订单状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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

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

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 looked all over the internet and have not found anything that can help me accomplish this. Any help would be greatly appreciated.

推荐答案

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

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

Add this code into theme's functions.php

这篇关于在特定时间过后自动更改 WooCommerce 订单状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆