Change order of orders in Woocommerce based on how much time is left by the date the order should be ready

Viewed 625

I need to change order of orders in Woocommerce admin page. I have some custom column with date / time and based on this data I will change the order of orders. It is not stricly date / time filtering (from oldest to newest or vice versa).

The decisive factor is how much time is left by the date the order should be ready. In my case, it's one hour. At that point, the order should be at the top of the list, regardless of whether it was created, for example, a week ago.

Examples:

1) Default ordering (newest on top, oldest on bottom)

+--------------------------------+------------+---------+--------------------+-------+
| ACTUAL DATE: 28.5.2020 - 15:00 |            |         |                    |       |
+--------------------------------+------------+---------+--------------------+-------+
| ORDER                          | DATE       | STATUS  | DATE/TIME (custom) | PRICE |
| Frank                          | 28.05.2020 | PENDING | -                  | 100   |
| Lucy                           | 27.05.2020 | DONE    | -                  | 50    |
| Thomas                         | 27.05.2020 | DONE    | -                  | 80    |
| Jane                           | 26.05.2020 | PENDING | 28.5.2020 - 16:00  | 90    |
+--------------------------------+------------+---------+--------------------+-------+

2) One hour left to complete Jane's order

+--------------------------------+------------+---------+--------------------+-------+
| ACTUAL DATE: 28.5.2020 - 15:00 |            |         |                    |       |
+--------------------------------+------------+---------+--------------------+-------+
| ORDER                          | DATE       | STATUS  | DATE/TIME (custom) | PRICE |
| Jane                           | 26.05.2020 | PENDING | 28.5.2020 - 16:00  | 90    |
| Frank                          | 28.05.2020 | PENDING | -                  | 100   |
| Lucy                           | 27.05.2020 | DONE    | -                  | 50    |
| Thomas                         | 27.05.2020 | DONE    | -                  | 80    |
+--------------------------------+------------+---------+--------------------+-------+

3) Two hour left to complete Jane's order

+--------------------------------+------------+---------+--------------------+-------+
| ACTUAL DATE: 28.5.2020 - 14:00 |            |         |                    |       |
+--------------------------------+------------+---------+--------------------+-------+
| ORDER                          | DATE       | STATUS  | DATE/TIME (custom) | PRICE |
| Frank                          | 28.05.2020 | PENDING | -                  | 100   |
| Lucy                           | 27.05.2020 | DONE    | -                  | 50    |
| Thomas                         | 27.05.2020 | DONE    | -                  | 80    |
| Jane                           | 26.05.2020 | PENDING | 28.5.2020 - 16:00  | 90    |
+--------------------------------+------------+---------+--------------------+-------+

Could you please give me a hint how to start? What hooks in my child theme function.php should I use?

EDIT: Example code for adding new column:

// Add column
add_filter( 'manage_edit-shop_order_columns', 'custom_delivery_time' );
function custom_delivery_time( $columns ) {
    $new_columns = ( is_array( $columns ) ) ? $columns : array();
    unset( $new_columns[ 'order_actions' ] );

    $new_columns['custom_datetime'] = 'Date / Time';
    $new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];

    return $new_columns;
}

// Change order of columns, populate
add_action( 'manage_shop_order_posts_custom_column', 'custom_woo_admin_value', 2 );
function custom_woo_admin_value( $column ) {
    global $post, $the_order;

    if ( empty( $the_order ) || $the_order->get_id() != $post->ID ) {
        $the_order = wc_get_order( $post->ID );
    }

    if ( $column == 'custom_datetime' ) {
        $custom_datetime = $the_order->get_custom_datetime();
        echo empty($custom_datetime) ? '' : $custom_datetime;
    }
}
1 Answers

Ok. I solved it this way. I created a custom "priority" column, which contains a numeric value that determines how high in the list the order should be. I then sort orders according to this custom column from the smallest (highest priorty) to the largest. Whenever a admin "orders list" page is updated, the appropriate priority is selected according to the time of delivery. I'm only interested in not finished orders, so there is not much to process.

I would still like to hear if someone comes up with a better solution.

Related