0

I am using the following code to change the status of an order in a woocommerce site when a given field 'ship_by_date_dont_edit' is within 2 (or less) days of today's date (via cron job). Everything works great, but I also need to account for negative days, as sometimes we will get a last-minute rush order, and the "ship by date" will get set as a date in the past. Upon testing, it does not seem days of less than 0 are included, thus the status does not change for those orders. I'm hoping to get some help so that my function works for dates within 2 days (or less, negative included).

Below is the original (messy) code:

//==== status change to 'preparing to ship'
add_action( 'new_cron_status_update_lessthan_two_days', 'wp_order_status_update', 10, 1 );
function wp_order_status_update() {
    global $wpdb;
    $my_query = "SELECT * FROM wp_wc_order_stats where STATUS='wc-processing'";
    $result2 = $wpdb->get_results($my_query);
    foreach ($result2 as $results2) {
        $order_id = $results2->order_id;
        $thisacof7 = get_post_meta( $order_id, 'ship_by_date_dont_edit', true );
        $date1=$thisacof7; // date("Y-m-d h:i:s");
        $thisdate=date("Y-m-d h:i:s");
        $dateStart = new DateTime($date1);
        $datetoday = new DateTime($thisdate);
        $dteDiff = $dateStart->diff($datetoday);
        $Diff = $dteDiff->format("%a");
        if($Diff<2) {
            $order = new WC_Order($order_id);
            if (!empty($order)) {
                 $order->update_status( 'wc-preparing-to-ship' );
            }
        }
    }
}

I tried adding a "$Diff->invert;" variable and using an else function to test if the result is a 1 (shouldn't this indicate a negative number?), but it then made all my orders change, even those with dates more than 60 days into the future, so I clearly did something wrong there.

            $resultPos = $Diff->invert;     
                elseif ($resultPos=1) {
        $order = new WC_Order($order_id);
        if (!empty($order)) {
             $order->update_status( 'wc-preparing-to-ship' );
        }
    }
  • Try `if( time() - $dateStart->getTimestamp() < 86400*2 ) { /* ... */ }` – Jared Dec 13 '22 at 23:54
  • Unfortunately using this results in all future orders getting changed (dates 60+ days out). – Matthew Bernier Dec 14 '22 at 04:31
  • `DateTime::diff` does _not_ give you a negative number of days in this situation - but will set the `invert` property to 1. https://www.php.net/manual/en/class.dateinterval.php – CBroe Dec 14 '22 at 07:35
  • @MatthewBernier what exact date/time range do you need? Two days from past plus lets say an hour in the future: `$diff = time() - $dateStart->getTimestamp(); if( $diff < 86400*2 and $diff > -3600 ) { /* ... */ }` – Jared Dec 14 '22 at 10:05
  • @CBroe, I was mistaken in what I wrote, but in the code, you can see I tried to define that precise scenario in the elseif condition. However, when used, it appears all results end up being 1, or at least all orders change as a result, no matter how far into the future they are, so I am still missing something there... `$resultPos = $Diff->invert; elseif ($resultPos=1)` – Matthew Bernier Dec 14 '22 at 15:21
  • If that is your actual current code, you should only get a parse error - you can not have `elseif` without a `}` before it. – CBroe Dec 14 '22 at 15:31
  • In the "negative" direction, you don't have this 2 day restriction, correct? If the date was set to 5 or 17 days in the past, you would still want to change the status? Then `if($dteDiff->invert || $dteDiff->format("%a") < 2)` should do the trick. – CBroe Dec 14 '22 at 15:34
  • Thanks, I'll give that a try. And for reference, the actual code had the necessary bracket, I just split it up for the sake of the post and forgot to include it. – Matthew Bernier Dec 14 '22 at 16:51
  • Good news, figured it out! Turns out when I was calculating the difference using date_diff, I was returning the absolute number as %a in $Diff object (positive integer). By adding the %r format in front of %a, I was able to return the positive or negative value, thus the invert function worked, resulting in a "1" for all negatives. Here is the updated code: `$dteDiff = $datetoday->diff($dateStart); $Diff = $dteDiff->format("%r%a"); $resultPos = $dteDiff->invert; $daysdiff = (int)$Diff; if($Diff->invert || $Diff < 2) {` – Matthew Bernier Dec 14 '22 at 19:01

1 Answers1

0

Here is the final updated (working) code. Just needed to apply %r in format to reflect negative numbers and use the $Diff variable in my invert function in order to return 1 for a negative number

//==== status change to 'preparing to ship'
add_action( 'new_cron_status_update_lessthan_two_days', 'wp_order_status_update', 10, 1 );
function wp_order_status_update() {
    global $wpdb;

    $my_query = "SELECT * FROM wp_wc_order_stats where STATUS='wc-processing'";
    $result2 = $wpdb->get_results($my_query);
    foreach ($result2 as $results2) {
        $order_id = $results2->order_id;
        $thisacof7 = get_post_meta( $order_id, 'ship_by_date_dont_edit', true );
        $date1=$thisacof7; // date("Y-m-d h:i:s");
        $thisdate=date("Y-m-d h:i:s");
        $dateStart = new DateTime($date1);
        $datetoday = new DateTime($thisdate);
        $dteDiff = $datetoday->diff($dateStart);
        $Diff = $dteDiff->format("%r%a");
        $resultPos = $dteDiff->invert;
        $daysdiff = (int)$Diff;
        if($Diff->invert || $Diff < 2) {
        $order = new WC_Order($order_id);
            if (!empty($order)) {
                 $order->update_status( 'wc-preparing-to-ship' );
            }
        }
    }
}
KargWare
  • 1,746
  • 3
  • 22
  • 35