结帐后如何计算订单总额

时间:2019-04-01 11:23:24

标签: php wordpress woocommerce orders totals

我在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 );
}

但是我的计算不起作用,因此我无法进行计算。

有什么建议或帮助吗?

3 个答案:

答案 0 :(得分:0)

您是否曾经两次都需要两种税率?否则,我认为一个税收变量就足够了,就像这样:

2147483647*2 == -2

另外,您的意思是您的计算正常吗?

答案 1 :(得分:0)

我认为您没有初始化变量set *(0x24040000 as *mut i32) = 0x0000CAFE $percent10

尝试一下:

$percent4

答案 2 :(得分: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 );
}