获取自定义计算作为 WooCommerce 管理员批量订单操作

Get a custom calculations as WooCommerce admin bulk orders action

我想让订单总计符合计算公式。

我有 driver 人的薪水是按订单计算的。他们交付已付款和未付款的订单。所以他们从 customers.At 一天结束时收取未付的鳕鱼付款,我计算我必须从他们那里收取多少,从金额中扣除他们的工资。(这种情况每天都会发生)

公式为 [ ('cod' 订单总数 ) - (已交付订单数) x 1.5 ]

这是一个例子

假设我们收到了 3 个订单并且 driver 收货了.. 所以在一天结束时 driver 必须带回现金.. 我想要想知道我要从他那里扣多少工资

order A : total = 50 : payment method /cod
order B : total = 10 : payment method /cod
order C : total = 40 : payment method /credit card

    the calculation = [ 'cod' orders totals - (selected orders number ) x 1.5 ]

the calculation = [ ( 50 + 10 ) - (3 ) x 1.5 ] = 55.5

结果是 (55.5) 是我必须从 driver

收取的金额

我不知道这是否必须在同一管理订单页面或新页面上.. 我想象在 WC 的订单管理部分或单独的页面上,我可以从订单列表中检查我想要计算的订单,以及从我选择的批量操作下拉框中检查订单,例如 [计算 1] ,和然后显示结果

您可以使用以下代码在批量订单选择中计算从您问题中定义的 driver 收取的金额:

// 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>'
        );
    }
}

代码进入活动 child 主题(或活动主题)的 function.php 文件。测试和工作。

此自定义批量操作(在订单列表批量操作下拉列表中):

可关闭消息框中的计算结果: