我在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 );
}
但是我的计算不起作用,例如我无法做到。
请提供任何建议或帮助?
有
没有一次你会同时拥有两种税率?否则,我认为一个税收变量就足够了,如下所示:
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
另外,您的计算是什么意思?
我认为您没有启动变量$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 );
}
因为您使用了错误的税收函数参数。你可以试试这个
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 );
}