问题
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