douchongzhang9267 2017-10-10 23:45
浏览 77
已采纳

在WooCommerce中隐藏特定用户角色的增值税

In WooCommerce I have some custom code to hide VAT for specific user roles, and it is working perfectly for all roles except for one called Platinum, where it doesn't hide the VAT but remains as all default user roles.

What I need is for this code to hide VAT for platinum also - it does for the other roles I listed

How can I make it work for my "platinum" user role too?

This is the code I use:

/**
 * 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
 )
);
  • 写回答

2条回答 默认 最新

  • doucuan5365 2017-10-11 01:40
    关注

    Update 2

    1) Your conditional function

    First current_user_can() is not recommended to be used with user roles directly and can only have one capability (or in your case one user role) at the time…

    Also in WooCommerce 3+ you will have to use WC()->customer->set_is_vat_exempt( true ); instead of WC()->customer->is_vat_exempt = true;

    You are using a repetitive conditional function based on your special user roles 3 times. So I have set a custom conditional function that should work perfectly:

    ## 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
    }
    

    And (your others functions):

    ## Function that will check for user role and turn off VAT/tax for that role
    add_action( 'template_redirect', 'wc_diff_rate_for_user', 1 );
    function wc_diff_rate_for_user() {
        // check for the user role and set the customer object to have no VAT
        if ( is_special_user_role() )
            WC()->customer->set_is_vat_exempt( true ); // Updated HERE
    }
    
    ## Function that filters the variable product hash based on user
    add_filter( 'woocommerce_get_variation_prices_hash', 'wc_get_variation_prices_hash_filter', 1, 3 );
    function wc_get_variation_prices_hash_filter( $hash, $item, $display ) {
        // check for the user role and clear key 2, which is where taxes are
        if ( is_special_user_role() )
            $hash['2'] = array();
    
        return $hash; // return the hash
    }
    
    ## Function that removes the price suffix (inc. Tax) from variable products based on role
    add_filter( 'woocommerce_get_price_suffix', 'wc_get_price_suffix_filter', 10, 2 );
    function wc_get_price_suffix_filter( $price_display_suffix, $item ) {
        // check for the user role return blank if it matches
        if ( is_special_user_role() )
            $price_display_suffix = '';
    
        return $price_display_suffix;
    }
    

    Code goes in function.php file of your active child theme (active theme or in any plugin file).

    All code is tested and works.


    2) For your user roles creation

    Instead you could use the excellent and very complete User Role Editor (free) plugin, that will allow you to create user roles, fine tune and check their capabilities (for each created role).

    For your user roles creation code, you should run it only once. I have revisited it, making some changes and compacted it.

    ## 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();
    

    Code goes in function.php file of your active child theme (active theme or in any plugin file).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?