skip to Main Content

Auto change order status from processing to Preparing in Woocommerce 1 day after placing order.

Auto change order status from processing to Preparing in Woocommerce 1 day after placing order.

2

Answers


  1. Chosen as BEST ANSWER
    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->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 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');
                    }
                }
            }
        }
    

  2. you can use this code in 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' );
                    }
                }
            }
    }
    
    

    You can set this function in the cron job too in your Cpanel.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search