php - 根据 WooCommerce 订单中的用户角色显示自定义税收总计行
问题描述
我正在 WooCommerce 中建立一个网上商店,我需要在订单电子邮件中显示正确的税金。我有 2 个不同的用户角色:“私人”和“企业”
以下代码进行税收计算并将其添加到新行。但是,对于“业务”用户角色,我需要输出为 0DKK (因为他们支付的价格不含税):
// 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.25 );
$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' => __('Moms udgør:','woocommerce'),
'value' => $total_tax_amount
) );
$total_rows['order_total']['value'] = wc_price( $gran_total );
return $total_rows + $tax_row;
}
任何帮助将不胜感激。
解决方案
对于“业务”用户角色,以下内容会将您的自定义总税行更改为零:
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 ){
$user = $order->get_user(); // Gets WP_User Object from order
$total = $order->get_total(); // Gets order grand total. incl. taxes
// Clean total row displayed taxes
$total_rows['order_total']['value'] = is_wc_endpoint_url() ? wc_price($total) : strip_tags(wc_price($total));
// Get total tax formatted from a defined tax rate based on user roles
$total_tax = in_array('business', $user->roles) ? wc_price(0) : wc_price($total - ($total / 1.25));
// Insert total tax row
$total_rows['order_tax_total'] = array(
'label' => __('Moms udgør:','woocommerce'),
'value' => is_wc_endpoint_url() ? $total_tax : strip_tags($total_tax),
);
return $total_rows;
}
它应该工作。
推荐阅读
- circleci - 修改 CircleCI 全局环境变量
- python-3.x - 带有 f 字符串的字典的字符串格式
- vue.js - PhpStorm 中的 Vue 模板快捷方式
- c# - 为什么 SignalR 需要添加“application/octet-stream”类型的响应压缩?
- c# - 如何对两个不同的程序集进行序列化和反序列化?
- ejs - 为什么按钮 onClick 在 ejs 文件中不起作用?
- heroku - 下一个 Js 的部署构建失败
- laravel - 部署更新的 laravel 应用程序 - 错误:无法安装系统包 - 已解决
- uart - 尽管没有“uart_write_bytes(...)”调用,ESP32C3 在复位后打印到串行
- firebase - 为什么要使用 Firebase 安全规则?