X时间过去后更改WooCommerce订单状态 [英] Change WooCommerce order status after X time has passed

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

问题描述

经过四处搜寻,我只发现了另一个接近的问题,但没有有效的答案.

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

我想在24小时后自动将WooCommerce中的订单状态从已完成"更改为已取消".

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.

我已经尝试过此处给出的答案,但无法使其正常工作:在特定时间过去后自动更改WooCommerce订单状态吗?

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

以前的答案代码是否需要在cron作业或类似的作业中定期运行?

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

推荐答案

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

Add this code into themes functions.php

您可以更改天数(if($ int> 1)):在此示例中:1天

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

(wp_)是table_prefix

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

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

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