php - 在 WooCommerce 中为免税客户禁用税
问题描述
我为我的 WooCommerce 安装启用了税收。它计算居住在我所在州的所有客户的税款(根据他们的送货地址)。这是 WooCommerce 的默认设置。
有些客户是免税的,不应该被征税。我在用户个人资料中创建了一个自定义字段,我可以在其中选中一个框来免除客户的税款。这可以正常工作。
我试图找到一个挂钩,我可以在其中使用该选择来禁用税收,但我拥有的代码导致结帐页面对于所有用户都是空白的。不显示错误消息。
我的functions.php代码如下:
///////////////////////////////////////
/* Tax exempt customers */
///////////////////////////////////////
// Add tax exempt custom user field in admin
add_action( 'show_user_profile', 'add_customer_tax_exempt_checkbox', 10 );
add_action( 'edit_user_profile', 'add_customer_tax_exempt_checkbox', 10 );
function add_customer_tax_exempt_checkbox( $user )
{
?>
<h3><?php _e("Tax status"); ?></h3>
<table class="form-table">
<tr>
<th><?php _e("Tax exempt"); ?></th>
<td>
<?php
woocommerce_form_field( 'tax_exempt', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Allowed'),
), get_user_meta( $user->id, 'tax_exempt', true ) );
?>
</td>
</tr>
</table>
<?php
}
// Save allowed custom user field in admin
add_action( 'personal_options_update', 'save_customer_tax_exempt_checkbox' );
add_action( 'edit_user_profile_update', 'save_customer_tax_exempt_checkbox' );
function save_customer_tax_exempt_checkbox( $user_id )
{
if ( current_user_can( 'edit_user', $user_id ) ) {
update_user_meta( $user_id, 'tax_exempt', isset($_POST['tax_exempt']) ? '1' : '0' );
}
}
// Enabling or disabling tax calculation at checkout
add_filter( 'woocommerce_product_tax_class', 'disable_tax_calculation' );
function disable_tax_calculation( $tax_class, $product ) {
if ( get_user_meta( get_current_user_id(), 'tax_exempt', true ) ) {
$tax_class = 'Zero Rate';
}
return $tax_class;
}
///////////////////////////////////////
/* END: Tax exempt customers */
///////////////////////////////////////
解决方案
由于 Woocommerce 3woocommerce_product_tax_class
钩子已被弃用并已被替换。我在下面更新了您的第三个功能:
// Add tax exempt custom user field in admin
add_action( 'show_user_profile', 'add_customer_tax_exempt_checkbox', 10 );
add_action( 'edit_user_profile', 'add_customer_tax_exempt_checkbox', 10 );
function add_customer_tax_exempt_checkbox( $user )
{
?>
<h3><?php _e("Tax status"); ?></h3>
<table class="form-table">
<tr>
<th><?php _e("Tax exempt"); ?></th>
<td>
<?php
woocommerce_form_field( 'tax_exempt', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Allowed'),
), get_user_meta( $user->id, 'tax_exempt', true ) );
?>
</td>
</tr>
</table>
<?php
}
// Save allowed custom user field in admin
add_action( 'personal_options_update', 'save_customer_tax_exempt_checkbox' );
add_action( 'edit_user_profile_update', 'save_customer_tax_exempt_checkbox' );
function save_customer_tax_exempt_checkbox( $user_id )
{
if ( current_user_can( 'edit_user', $user_id ) ) {
update_user_meta( $user_id, 'tax_exempt', isset($_POST['tax_exempt']) ? '1' : '0' );
}
}
// Enabling or disabling tax calculation at checkout
add_filter( 'woocommerce_product_get_tax_class', 'disable_tax_calculation', 10, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'disable_tax_calculation', 10, 2 );
function disable_tax_calculation( $tax_class, $product ) {
if ( get_user_meta( get_current_user_id(), 'tax_exempt', true ) ) {
$tax_class = 'Zero Rate';
}
return $tax_class;
}
代码在您的活动子主题(或活动主题)的functions.php 文件中。它应该更好地工作。
推荐阅读
- python - 试图将浮点数转换为小数
- java - 如何在 Java 的方法中使用构造函数参数?
- entity-framework - EF Code First 将外键插入/更新限制为给定的 ReferenceType
- swift - 致命错误:刷新集合视图时索引超出范围
- asp.net - 当我传递 DTO 然后在 Web API 2 中转换为模型时如何更新数据库
- jquery - 在 jQuery 选项中使用 Hiddenfield 值
- ios - 如何让 SKEmitterNode 在 SwiftUI 中工作?
- javascript - jQuery::addClass() 行为异步?不会立即发生并被 confirm() 阻止
- opencart - 如何在 Opencart 上添加 HTML?
- xml - 使用 Go 解析 XML 文件