【问题标题】:Get a custom calculations as WooCommerce admin bulk orders action获取自定义计算作为 WooCommerce 管理员批量订单操作
【发布时间】:2019-09-08 07:51:04
【问题描述】:

我想让订单总额按照计算公式计算。

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

公式为 [ ('cod' 订单总数) - (已交付订单数) 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 的订单管理部分或单独的页面上,我可以从我要计算的订单列表和我选择的批量操作下拉框中检查订单,例如 [CALCULATION 1],并且然后显示结果

【问题讨论】:

  • @LoicTheAztec 你能帮我解决这个问题吗

标签: php wordpress woocommerce hook-woocommerce orders


【解决方案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 文件中。测试和工作。

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

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

【讨论】:

  • @Ibrahim 我已经更新了我的代码和屏幕截图……它在消息中包含已处理的订单……您甚至可以同时将订单状态更改为“已完成”……
  • @Ibrahim 我刚刚更新了代码以获取订单号而不是 ID...如果您想更新订单状态,请取消注释 // $order-&gt;update_status("completed"); 删除 //... 无法获取链接,因为数据是通过 URL(GET 方法)传递的
猜你喜欢
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
  • 2019-08-29
  • 1970-01-01
  • 2020-06-15
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多