在WooCommerce中,我尝试在订单明细中的总税额下方添加税款。我想向订单总计添加一行。但是,下面的代码不起作用:
WHERE JSON_VALUE(columnName, $.info.address[1].town) = 'Belgrade'
我也尝试过这种方法,但是它也不起作用:
//Show tax in order details
add_action( 'woocommerce_checkout_create_order', 'func_new_total', 20, 1 );
function func_new_total( $order ) {
// Get order total
$total = $order->get_total();
//Calculate tax
$tax = $total * 0.2;
//Add tax on new line below total
$new_total = $total . '<br>' . 'herav Mva kr' . $tax;
$order->set_total($new_total);
}
答案 0 :(得分:1)
您可以使用以下代码,在订单总计(用于订单和电子邮件通知)下方的单独一行中显示总税额:
// Add total taxes as a separated line before order total on orders and emails
add_filter( 'woocommerce_get_order_item_totals', 'insert_custom_line_order_item_totals', 10, 3 );
function insert_custom_line_order_item_totals( $total_rows, $order, $tax_display ){
// Display only the gran total amount
$gran_total = wc_price( $order->get_total() );
$total_rows['order_total']['value'] = is_wc_endpoint_url() ? $gran_total : strip_tags( $gran_total );
$total_tax_amount = wc_price( $order->get_total_tax() );
$total_tax_amount = is_wc_endpoint_url() ? $total_tax_amount : strip_tags( $total_tax_amount );
// Create a new row for total tax
$tax_row = array( 'order_tax_total' => array(
'label' => __('Herav MVA:','woocommerce'),
'value' => $total_tax_amount
) );
$total_rows['order_total']['value'] = $gran_total;
return $total_rows + $tax_row;
}
或使用您的自定义总税20%计算:
// Add total taxes as a separated line before order total on orders and emails
add_filter( 'woocommerce_get_order_item_totals', 'insert_custom_line_order_item_totals', 10, 3 );
function insert_custom_line_order_item_totals( $total_rows, $order, $tax_display ){
// Display only the gran total amount
$gran_total = (float) $order->get_total();
$total_rows['order_total']['value'] = is_wc_endpoint_url() ? $total_html : strip_tags( $total_html );
// Custom tax calculation (for 20% tax rate)
$total_tax_amount = wc_price( $gran_total - $gran_total / 1.2 );
$total_tax_amount = is_wc_endpoint_url() ? $total_tax_amount : strip_tags( $total_tax_amount );
// Create a new row for total tax
$tax_row = array( 'order_tax_total' => array(
'label' => __('Herav MVA:','woocommerce'),
'value' => $total_tax_amount
) );
$total_rows['order_total']['value'] = wc_price( $gran_total );
return $total_rows + $tax_row;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。