Change WooCommerce order status after X time has passed

不羁的心 提交于 2021-01-29 09:32:16

问题


Having searched around, i only found one other close question, but no valid answer.

I would like to automatically change the order status in WooCommerce from Completed to Cancelled after 24 hours.

Ideally this would only be for orders that contain a Virtual Product too.

Ive tried the answer given here, but couldnt get it to work: Automatically change WooCommerce order status after specific time has passed?

Would the previous answers code need to be run regularly in a cron job or something similar?


回答1:


Add this code into themes functions.php

You can change days ( if($int>1) ) : in this example : 1 day

( wp_ ) is table_prefix

add_action('init', 'wp_orders');
function wp_orders()
{

    global $wpdb;

$my_query = "SELECT * FROM wp_wc_order_stats where STATUS='wc-processing'";
        $val123 = $wpdb->get_row($my_query, OBJECT);
        $result2 = $wpdb->get_results($my_query);
        foreach ($result2 as $results2) {

            $date1 = $results2->date_created_gmt;
            $order_id = $results2->order_id;

            $date2=date("Y-m-d h:i:s");

$dteStart = new DateTime($date1);
$dteEnd   = new DateTime($date2);
$dteDiff  = $dteStart->diff($dteEnd);
$Diff = $dteDiff->format("%d");
            $int = (int)$Diff;
            if($int>1)
            {
                $order = new WC_Order($order_id);
                if (!empty($order)) {
                    $order->update_status( 'completed' );
                }
            }
        }
}


来源:https://stackoverflow.com/questions/58055471/change-woocommerce-order-status-after-x-time-has-passed

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