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

时间:2019-09-08 07:51:04

标签: php wordpress woocommerce hook-woocommerce orders

我想让订单总数归入计算公式。

我有一些司机,他们的工资是每个订单的工资。他们交付已付款和未付款的订单。因此,他们从客户那里收取未付的鳕鱼付款。最终,我计算出我必须从他们那里收取多少钱,然后从金额中扣除他们的薪水。(这种情况每天都会发生)

公式为[(“鳕鱼”订单总数)-(已交付订单数量)x 1.5]

这是一个例子

假设我们已经收到3份订单,并且司机收了它们以便交付..因此,在一天结束时,司机不得不将现金带回..在这里,我想知道我必须从他那里收取多少钱扣除他的工资

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)是我必须向驾驶员收取的金额

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

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码,使您可以选择批量订单来计算问题中定义的从驱动程序收取的金额:

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

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。

此自定义批量操作(在“订单”列表上的批量操作下拉菜单中):

enter image description here

可忽略的消息框中的计算结果:

enter image description here