【问题标题】:Auto change order status from processing to Preparing in Woocommerce在 Woocommerce 中自动将订单状态从处理更改为准备中
【发布时间】:2023-01-28 15:50:23
【问题描述】:

下订单 1 天后,在 Woocommerce 中自动将订单状态从处理中更改为准备中。

下订单后 1 天,在 Woocommerce 中自动将订单状态从处理中更改为准备中。

【问题讨论】:

  • 尝试使用 cron Job 和 Hooks。点击此链接link

标签: wordpress woocommerce


【解决方案1】:

您可以在 functions.php 中使用此代码


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)) {

               // change your status to wc-preparing

                    $order->update_status( 'wc-preparing' );
                }
            }
        }
}

您也可以在 Cpanel 的 cron 作业中设置此功能。

【讨论】:

  • 谢谢我怎么能跳过星期天
  • if(in_array(date('D'),['Sun'])) { return; }在循环之前添加这个条件
  • 嗨,我已经添加了上面的代码,但它对我不起作用我认为出了点问题,仍然只显示在处理中
  • add_action('init', 'wp_orders');函数 wp_orders() { 全局 $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)$差异;如果($int>1) { $order = new WC_Order($order_id);如果 (!empty($order)) { $order->update_status( 'wc-accepted' ); } } } }
【解决方案2】:
add_action('init', 'change_order_status');
    function change_order_status() {
        global $wpdb;

        // Query for orders with a status of "wc-processing"
        $my_query = "SELECT * FROM wp_wc_order_stats WHERE STATUS='wc-processing'";
        $result = $wpdb->get_results($my_query);

        // Iterate through the results
        foreach ($result as $order) {
            $order_id = $order->order_id;
            $order_date = $order->date_created_gmt;

            // Get the current date
            $current_date = date("Y-m-d h:i:s");

            // Calculate the difference in days between the order date and the current date
            $dteStart = new DateTime($order_date);
            $dteEnd = new DateTime($current_date);
            $dteDiff = $dteStart->diff($dteEnd);
            $diff_in_days = $dteDiff->format("%d");

            // Compare the difference in days to 1
            if ($diff_in_days >= 1) {
                $order = new WC_Order($order_id);
                if (!empty($order)) {
                    // Change the order status to "wc-accepted"
                    $order->update_status('wc-accepted');
                }
            }
        }
        // Query for orders with a status of "wc-accepted"
        $my_query = "SELECT * FROM wp_wc_order_stats WHERE STATUS='wc-accepted'";
        $result = $wpdb->get_results($my_query);

        // Iterate through the results
        foreach ($result as $order) {
            $order_id = $order->order_id;
            $order_date = $order->date_created_gmt;

            // Get the current date
            $current_date = date("Y-m-d h:i:s");

            // Calculate the difference in days between the order date and the current date
            $dteStart = new DateTime($order_date);
            $dteEnd = new DateTime($current_date);
            $dteDiff = $dteStart->diff($dteEnd);
            $diff_in_days = $dteDiff->format("%d");

            // Compare the difference in days to 5
            if ($diff_in_days >= 5) {
                $order = new WC_Order($order_id);
                if (!empty($order)) {
                    // Change the order status to "wc-preparing"
                    $order->update_status('wc-preparing');
                }
            }
        }
        // Query for orders with a status of "wc-preparing"
        $my_query = "SELECT * FROM wp_wc_order_stats WHERE STATUS='wc-preparing'";
        $result = $wpdb
        // Iterate through the results
        foreach ($result as $order) {
            $order_id = $order->order_id;
            $order_date = $order->date_created_gmt;

            // Get the current date
            $current_date = date("Y-m-d h:i:s");

            // Calculate the difference in days between the order date and the current date
            $dteStart = new DateTime($order_date);
            $dteEnd = new DateTime($current_date);
            $dteDiff = $dteStart->diff($dteEnd);
            $diff_in_days = $dteDiff->format("%d");

            // Compare the difference in days to 6
            if ($diff_in_days >= 6) {
                $order = new WC_Order($order_id);
                if (!empty($order)) {
                    // Change the order status to "wc-ready-to-ship"
                    $order->update_status('wc-ready-to-ship');
                }
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2018-08-01
    • 2019-06-01
    • 2022-10-14
    • 2016-08-04
    • 2018-02-27
    • 2020-08-11
    • 2021-07-26
    • 2017-05-24
    • 2021-04-08
    相关资源
    最近更新 更多