Check the status of recent uncompleted orders in Woocommerce 3

If there is experience, tell me, please, how to do it, or plan of action to go in the right direction.

  1. It is necessary for CRON, checking the statuses in the external delivery service (but it does not matter).
  2. Get uncompleted orders or alternately get one uncompleted order.

Example: We have 3-5 uncompleted orders (last 24 hours). I create some function for check the status of an uncompleted order. Get first order, checking status, if the external service status is changed, then I change it in the store. The action is completed.

Statuts for check: Hold On, Processing.

Interested in paragraph 2.

I found an argument here, maybe it fits:

// Get refunds in the last 24 hours.
$args = array(
    'type' => 'shop_order_refund',
    'date_created' => '>' . ( time() - DAY_IN_SECONDS ),
);
$orders = wc_get_orders( $args );

Upvotes: 1

Views: 705

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253978

Update 2 (Checking orders within the 24 hours from creation date)

You can use this very light SQL query embedded in a utility function, that will get all orders IDs which have a status "On Hold" and "Processing" within the 24 hours from order creation date:

function get_order_ids_to_check(){
    global $wpdb;

    return $wpdb->get_col( "
        SELECT p.ID
        FROM {$wpdb->prefix}posts as p
        WHERE p.post_type LIKE 'shop_order'
        AND p.post_status IN ('wc-on-hold','wc-processing')
        AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - 86400)
    " );
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.


USAGE - You can use it anywhere, in another function like in this fake code example:

// Get the Orders IDs to check
$orders_ids = get_order_ids_to_check();

// Loop through each order Ids
foreach( $orders_ids as $order_id ){
    // Get the delivery order ID related to your external shipping service
    $delivery_order_id = get_post_meta( $order_id, 'delivery_order_id', true );
}

Upvotes: 2

Related Questions