如何解决如何根据用户角色和产品类别在 WooCommerce 中激活“零税率”税级
在我正在开发的 WooCommerce 网站上,我不仅要向最终客户销售产品,还要向经销商销售产品。
问题是转销商免税,因此我需要一个自定义功能来为某些类型的用户激活零税率。
所以,我的问题是我的代码运行良好(当用户是管理员或经销商时),但我不知道如何做,以便这些更改仅反映在一个产品类别(葡萄酒)中。
这是我使用的代码:
function wc_diff_rate_for_user( $tax_class,$product ) {
$current_user = wp_get_current_user();
$current_user_data = get_userdata($current_user->ID);
if ( in_array( 'administrator',$current_user_data->roles ) || in_array( 'reseller',$current_user_data->roles ) )
$tax_class = 'Zero Rate';
return $tax_class;
}
add_filter( 'woocommerce_product_get_tax_class','wc_diff_rate_for_user',10,2 );
add_filter( 'woocommerce_product_variation_get_tax_class',2 );
如何扩展此代码以便它也检查产品类别?
解决方法
您可以使用 has_term()
WordPress 条件函数来检查以下类别:
function filter_woocommerce_product_get_tax_class( $tax_class,$product ) {
// Specific categories: the term name/term_id/slug. Several could be added,separated by a comma
$categories = array( 'wine' );
// Has term (product category)
if ( has_term( $categories,'product_cat',$product->get_id() ) ) {
// Get current user
$user = wp_get_current_user();
// Roles to check
$roles_to_check = array( 'administrator','reseller' );
// User role is found?
if ( count( array_intersect( $user->roles,$roles_to_check ) ) > 0 ) {
$tax_class = 'Zero rate';
}
}
return $tax_class;
}
add_filter( 'woocommerce_product_get_tax_class','filter_woocommerce_product_get_tax_class',10,2 );
add_filter( 'woocommerce_product_variation_get_tax_class',2 );
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。