Automatically change WooCommerce order status after specific time has passed?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 09:14:22

问题


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.


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

Add this code into theme's functions.php



来源:https://stackoverflow.com/questions/42422507/automatically-change-woocommerce-order-status-after-specific-time-has-passed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!