Php 隐藏WooCommerce中特定用户角色的增值税
在WooCommerce中,我有一些自定义代码来隐藏特定用户角色的VAT,除了一个名为Platinum的角色外,它对所有角色都非常有效,该角色不隐藏VAT,但仍然作为所有默认用户角色 我需要的是,此代码还可以隐藏platinum的VAT-它可以用于我列出的其他角色 我怎样才能使它也适用于我的“白金”用户角色 这是我使用的代码:Php 隐藏WooCommerce中特定用户角色的增值税,php,wordpress,woocommerce,user-roles,tax,Php,Wordpress,Woocommerce,User Roles,Tax,在WooCommerce中,我有一些自定义代码来隐藏特定用户角色的VAT,除了一个名为Platinum的角色外,它对所有角色都非常有效,该角色不隐藏VAT,但仍然作为所有默认用户角色 我需要的是,此代码还可以隐藏platinum的VAT-它可以用于我列出的其他角色 我怎样才能使它也适用于我的“白金”用户角色 这是我使用的代码: /** * Function that will check for user role and turn off VAT/tax for that role */
/**
* Function that will check for user role and turn off VAT/tax for that role
*/
function wc_diff_rate_for_user() {
// check for the user role
if ( is_user_logged_in() && current_user_can( 'bronze', 'sølv', 'guld', 'platinum' ) ) {
// set the customer object to have no VAT
WC()->customer->is_vat_exempt = true;
}
}
add_action( 'template_redirect', 'wc_diff_rate_for_user', 1 );
/**
* Function that filters the variable product hash based on user
*/
function wc_get_variation_prices_hash_filter( $hash, $item, $display ) {
// check for the user role
if ( is_user_logged_in() && current_user_can( 'bronze', 'sølv', 'guld', 'platinum' ) ) {
// clear key 2, which is where taxes are
$hash['2'] = array();
}
// return the hash
return $hash;
}
add_filter( 'woocommerce_get_variation_prices_hash', 'wc_get_variation_prices_hash_filter', 1, 3 );
/**
* Function that removes the price suffix (inc. Tax) from variable products based on role
*/
function wc_get_price_suffix_filter( $price_display_suffix, $item ) {
// check for the user role
if ( is_user_logged_in() && current_user_can( 'bronze', 'sølv', 'guld', 'platinum' ) ) {
// return blank if it matches
return '';
}
// return if unmatched
return $price_display_suffix;
}
add_filter( 'woocommerce_get_price_suffix', 'wc_get_price_suffix_filter', 10, 2 );
//B2B Roller
add_role('bronze', __(
'Bronze'),
array(
'read' => false, // Allows a user to read
'create_posts' => false, // Allows user to create new posts
'edit_posts' => false, // Allows user to edit their own posts
)
);
add_role('sølv', __(
'Sølv'),
array(
'read' => false, // Allows a user to read
'create_posts' => false, // Allows user to create new posts
'edit_posts' => false, // Allows user to edit their own posts
)
);
add_role('guld', __(
'Guld'),
array(
'read' => false, // Allows a user to read
'create_posts' => false, // Allows user to create new posts
'edit_posts' => false, // Allows user to edit their own posts
)
);
add_role('platinum', __(
'Platinum'),
array(
'read' => false, // Allows a user to read
'create_posts' => false, // Allows user to create new posts
'edit_posts' => false, // Allows user to edit their own posts
)
);
不确定代码是如何工作的,因为函数当前用户可以接受一个参数: 我将用以下代码重写所有部分:
if ( is_user_logged_in() && current_user_can( 'bronze', 'sølv', 'guld', 'platinum' ) ) {
对这样的事情:
if ( is_user_logged_in() && ( current_user_can( 'bronze' ) || current_user_can( 'sølv' ) || current_user_can( 'guld' ) || current_user_can( 'platinum' ) ) ) {
更新2 1)您的条件函数 第一个不建议直接与用户角色一起使用,同时只能有一个功能(或者在您的情况下只有一个用户角色) 同样在WooCommerce 3+中,您必须使用
WC()->客户->设置增值税免税(true)代码>
而不是WC()->customer->is\u vat\u免税=真代码>
您正在使用基于特殊用户角色的重复条件函数3次。因此,我设置了一个自定义条件函数,该函数应能完美工作:
## The (repetitive) conditional function based on your "special" user roles
function is_special_user_role(){
if ( ! is_user_logged_in() ) return false;
$user = wp_get_current_user(); // current user
$user_roles = $user->roles; // It's always an array (as a user can have many roles)
// HERE your defined user roles
$defined_user_roles = array( 'bronze', 'sølv', 'guld', 'platinum' );
if ( count( array_intersect( $user_roles, $defined_user_roles ) ) > 0 ) return true;
else return false; // ==> Added update here
}
和(您的其他功能):
代码放在活动子主题(活动主题或任何插件文件)的function.php文件中
所有代码都经过测试并正常工作
2)用于创建用户角色
相反,您可以使用优秀且非常完整的,这将允许您创建用户角色,微调并检查其功能(针对每个创建的角色)
对于用户角色创建代码,应该只运行一次。我重新访问了它,做了一些更改并将其压缩
## Special user roles creation ==> this should be runned just once!
function special_ser_role_creation(){
// Your "Special" user roles slug / name pairs
$special_roles_array = array(
'bronze' => __( 'Bronze' ),
'sølv' => __( 'Sølv' ),
'guld' => __( 'Guld' ),
'platinum' => __( 'Platinum' ),
);
$roles_caps = array(
'read' => false, // Allows a user to read
'create_posts' => false, // Allows user to create new posts
'edit_posts' => false, // Allows user to edit their own posts
);
$existing_user_roles = array_keys( wp_roles()->get_names() );
// Iterating through each user roles and create them
foreach( $special_roles_array as $role_slug => $role_name ){
// If user role doesn't exist yet we create it
if (! in_array( $role_slug, $existing_user_roles) )
add_role( $role_slug, $role_name, $roles_caps );
}
}
// Run the function once (then comment it or remove it)
special_ser_role_creation();
代码放在活动子主题(活动主题或任何插件文件)的function.php文件中。当我用当前代码替换它时,它会出现错误。我已经在文章的顶部附加了错误,因为它太长,无法在这里发布。@eMikkelsen我已经更新了条件函数(在末尾)。请试一试。谢谢
## Special user roles creation ==> this should be runned just once!
function special_ser_role_creation(){
// Your "Special" user roles slug / name pairs
$special_roles_array = array(
'bronze' => __( 'Bronze' ),
'sølv' => __( 'Sølv' ),
'guld' => __( 'Guld' ),
'platinum' => __( 'Platinum' ),
);
$roles_caps = array(
'read' => false, // Allows a user to read
'create_posts' => false, // Allows user to create new posts
'edit_posts' => false, // Allows user to edit their own posts
);
$existing_user_roles = array_keys( wp_roles()->get_names() );
// Iterating through each user roles and create them
foreach( $special_roles_array as $role_slug => $role_name ){
// If user role doesn't exist yet we create it
if (! in_array( $role_slug, $existing_user_roles) )
add_role( $role_slug, $role_name, $roles_caps );
}
}
// Run the function once (then comment it or remove it)
special_ser_role_creation();