结账后如何计算订单总额
How to calculate order total after check out
我在 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 );
}
因为你用错了tax函数的参数。你可以试试这个
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 );
}
我在 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 );
}
因为你用错了tax函数的参数。你可以试试这个
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 );
}