如何在结账后计算订单总额

问题描述 投票:0回答:3

我在Woocommerce中计算了不同的自定义订单总额:

add_action( 'woocommerce_checkout_create_order', 'change_total_on_checking', 20, 1 );
function change_total_on_checking( $order ) {
    // Get order total
    $total = $order->get_total();

    $orderproduct = $order->get_items();
    $tax_rate     = WC_Tax::get_rates( $orderproduct );

    if ($tax_rate == "10") {
        $percent10 = $total * $tax_rate; 
    }

    if ( $tax_rate == "4" ){
        $percent4 = $total * $tax_rate;
    }

    ## -- fai check e calcoli -- ##
    $new_total = $total + $percent4 + $percent10; // <== Fake calculation

    // imposta un calcolo nuovo
    $order->set_total( $new_total );
}

但我的计算不起作用,我无法做到这一点。

有什么建议或帮助吗?

php wordpress woocommerce orders totals
3个回答
0
投票

你有没有时间同时享受两种税率?否则,我认为税收的一个变量就足够了,如下:

if ($tax_rate == "10") {
    $tax_total = $total * $tax_rate; 
}

if ( $tax_rate == "4" ){
    $tax_total = $total * $tax_rate;
}

## -- fai check e calcoli -- ##
$new_total = $total + $tax_total

另外,你的意思是你的计算是什么意思?


0
投票

我认为你没有启动变量$percent10$percent4

试试这个:

add_action( 'woocommerce_checkout_create_order', 'change_total_on_checking', 20, 1 );
function change_total_on_checking( $order ) {
    // Get order total
    $total = $order->get_total();

    $orderproduct = $order->get_items();
    $tax_rate     = WC_Tax::get_rates( $orderproduct );
    $percent10 = 0; // if $tax_rate != "10", will sum zero in $new_total
    $percent4 = 0; // if $tax_rate != "4", will sum zero in $new_total

    if ($tax_rate == "10") {
        $percent10 = $total * $tax_rate; 
    }

    if ( $tax_rate == "4" ){
        $percent4 = $total * $tax_rate;
    }

    ## -- fai check e calcoli -- ##
    $new_total = $total + $percent4 + $percent10; // <== Fake calculation

    // imposta un calcolo nuovo
    $order->set_total( $new_total );
}

0
投票

因为你使用了税功能的错误参数。你可以试试这个

add_action( 'woocommerce_checkout_create_order', 'change_total_on_checking', 20, 1 );
function change_total_on_checking( $order ) {
    // Get order total
    $total = $order->get_total();

    $orderproduct = $order->get_items();
    foreach($orderproduct as $product){
        $tax_rates     = WC_Tax::get_rates( $product->get_tax_class() );
        if($tax_rate = $tax_rates[1]['rate']){
            if ($tax_rate   == 10) {
                $percent10[] = $total * $tax_rate; 
            }

            if ($tax_rate  == 4) {
                $percent4[] = $total * $tax_rate;
            }
        }

    }
   /* */

    ## -- fai check e calcoli -- ##
    $new_total = $total + floatval((is_array($percent4))? array_sum($percent4): 0) + floatval((is_array($percent10))? array_sum($percent10): 0); // <== Fake calculation
    // imposta un calcolo nuovo
    $order->set_total( $new_total );
}
© www.soinside.com 2019 - 2024. All rights reserved.