You can use the following code that will allow you on bulk order selection to calculate the amount to collect from the driver as defined in your question:
// Display the custom actions on admin Orders bulk action dropdown
add_filter( 'bulk_actions-edit-shop_order', 'orders_bulk_action_delivery_collect_calc' );
function orders_bulk_action_delivery_collect_calc( $bulk_actions ) {
$bulk_actions['delivery-collect'] = __( "Calculate delivery collect", 'woocommerce' );
return $bulk_actions;
}
// Process the bulk action from selected orders
add_filter( 'handle_bulk_actions-edit-shop_order', 'delivery_collect_calc_bulk_action_edit_shop_order', 10, 3 );
function delivery_collect_calc_bulk_action_edit_shop_order( $redirect_to, $action, $post_ids ) {
if ( $action === 'delivery-collect' ) {
$order_numbers = []; // Initializing
$cod_orders_total = 0; // Initializing
$salary_per_order = 1.5;
foreach ( $post_ids as $post_id ) {
// Get Order status
$order = wc_get_order( $post_id );
if( $order->get_payment_method() === 'cod' ) {
$cod_orders_total += (float) $order->get_total();
}
// Order status change to "completed" (To enable uncomment the line below)
// $order->update_status("completed");
$order_numbers[] = $order->get_order_number(); // Adding processed order numbers to an array
}
$orders_count = count( $order_numbers );
$amount_to_collect = $cod_orders_total - ( $orders_count * $salary_per_order );
// Adding the right query vars to the returned URL
$redirect_to = add_query_arg( array(
'collect_action' => $action,
'processed_count' => $orders_count,
'proc_order_nums' => implode( ',', $order_numbers ),
'amount_to_collect' => $amount_to_collect,
), $redirect_to );
}
return $redirect_to;
}
// Display the results notice from bulk action on orders
add_action( 'admin_notices', 'set_delivery_collect_bulk_action_admin_notice' );
function set_delivery_collect_bulk_action_admin_notice() {
global $pagenow;
if ( 'edit.php' === $pagenow && isset($_GET['post_type']) && 'shop_order' === $_GET['post_type']
&& isset($_GET['collect_action']) && isset($_GET['processed_count'])
&& isset($_GET['proc_order_nums']) && isset($_GET['amount_to_collect']) ) {
$count_ids = intval( $_GET['processed_count'] );
$amount = floatval( $_GET['amount_to_collect'] );
$currency = get_woocommerce_currency_symbol();
printf( '<div class="notice notice-success fade is-dismissible"><p>' .
_n( "On %s selected order, the calculated amount to collect is %sProcessed order Id is %s",
"On the %s selected orders, the calculated amount to collect is %sProcessed orders Ids are %s.",
$count_ids, "woocommerce" ) . '</p></div>',
$count_ids,
'<code>' . number_format_i18n($amount, 2) . '</code> (' . $currency . ').</p><p>',
'<code>' . $_GET['proc_order_nums'] . '</code>'
);
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
This custom bulk action (on the Orders list bulk action dropdown):
The result of the calculation in a dismissible message box: