Skip to content
Advertisement

Get a custom calculations as WooCommerce admin bulk orders action

i want to make order totals go under a calculation formula.

i have drivers whom salary is per order. they deliver paid and unpaid orders. so They collect unpaid cod payments from the customers.At the end of the day i calculate how much do i have to collect from them deducting their salary from the amount.( this happen on daily basis)

the formula is [ (‘cod’ orders totals ) – (delivered orders Numbers) x 1.5 ]

this is an example

assume that we’ve received 3 orders and the driver collected them for delivery .. so at the end of the day the driver has to bring the cash back .. here i want to know how much i have to collect from him deducting his salary

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

the result is (55.5) is amount of money i have to collect from the driver

i dont know if this have to be on the same admin orders page or a new page .. i imagine that in the orders management section in WC or on the separate page , i can check the orders from the order list that i want to make the calculations for and from the bulk actions drop box i choose for example [ CALCULATION 1 ] , and then the result is shown

Advertisement

Answer

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):

enter image description here

The result of the calculation in a dismissible message box:

enter image description here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement