• Hello, we need to restrict a badge to be visible to only certain user roles. We had been given a code snippet to achieve this but the code no longer works:

    Hi,
    we are releasing a new version of YITH WooCommerce Badge Management (Version 1.3.5).
    You could upgrade it and add the following code to your theme functions.php:
    
    if ( !function_exists( 'yith_wcbm_customization_get_badge_by_user_role' ) ) {
        function yith_wcbm_customization_get_badge_by_user_role( $badge_html, $id_badge, $product_id ) {
            if ( 126 == $id_badge ) {
                $not_allowed_roles = array( 'administrator' );
                $current_user      = wp_get_current_user();
    
                if ( is_object( $current_user ) && !empty( $current_user->roles ) && is_array( $current_user->roles ) ) {
                    $match_role = array_intersect( $current_user->roles, $not_allowed_roles );
                    if ( count( $match_role ) > 0 )
                        $badge_html = '';
                }
    
            }
    
            return $badge_html;
        }
    
        add_filter( 'yith_wcbm_get_badge', 'yith_wcbm_customization_get_badge_by_user_role', 10, 3 );
    }
    This code will hide the badge with ID = 126 for administrators.
    
    Best Regards
    YITH Support

    Is there an updated snippet we can use?

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author YITHEMES

    (@yithemes)

    Hi there,
    the code you paste works on contrary compared what you achieve.
    Try using following code

    if ( !function_exists( 'yith_wcbm_customization_get_badge_by_user_role' ) ) {
        function yith_wcbm_customization_get_badge_by_user_role( $badge_html, $id_badge, $product_id ) {
            if ( 126 == $id_badge ) {
                if( is_user_logged_in() ){
                    $allowed_roles = array( 'administrator' );
                    $current_user      = wp_get_current_user();
                    if ( is_object( $current_user ) && !empty( $current_user->roles ) && is_array( $current_user->roles ) ) {
                        $match_role = array_intersect( $current_user->roles, $allowed_roles );
                        var_dump($match_role);die();
                        if ( count( $match_role ) == 0 )
                            $badge_html = '';
                    }
                }else{
                    $badge_html = '';
                }
            }
    
            return $badge_html;
        }
    
        add_filter( 'yith_wcbm_get_badge', 'yith_wcbm_customization_get_badge_by_user_role', 10, 3 );
    }

    This code allows you to show the badge only for user roles that you set.

    Thread Starter bt_dev

    (@biotrace)

    Hello – I tried using the following code:

    if ( !function_exists( 'yith_wcbm_customization_get_badge_by_user_role' ) ) {
        function yith_wcbm_customization_get_badge_by_user_role( $badge_html, $id_badge, $product_id ) {
            if ( 11655 == $id_badge ) {
                if( is_user_logged_in() ){
                    $allowed_roles = array( 'administrator' );
                    $current_user      = wp_get_current_user();
                    if ( is_object( $current_user ) && !empty( $current_user->roles ) && is_array( $current_user->roles ) ) {
                        $match_role = array_intersect( $current_user->roles, $allowed_roles );
                        var_dump($match_role);die();
                        if ( count( $match_role ) == 0 )
                            $badge_html = '';
                    }
                }else{
                    $badge_html = '';
                }
            }
    
            return $badge_html;
        }
    
        add_filter( 'yith_wcbm_get_badge', 'yith_wcbm_customization_get_badge_by_user_role', 10, 3 );
    }

    However, it breaks any page that has the specified badge applied to a product. This is what gets displayed in the browser when trying to view the product page with the badge applied:

    array(1) { [0]=> string(13) “administrator” }

    Also, will this code work with the premium version?

    Plugin Author YITHEMES

    (@yithemes)

    Hi there,
    I’m afraid for the mistake, you’re right.
    I forgot to remove a code that I had put for my test.

    var_dump($match_role);die();
    

    This is the correct code

    if ( !function_exists( 'yith_wcbm_customization_get_badge_by_user_role' ) ) {
        function yith_wcbm_customization_get_badge_by_user_role( $badge_html, $id_badge, $product_id ) {
            if ( 126 == $id_badge ) {
                if( is_user_logged_in() ){
                    $allowed_roles = array( 'administrator' );
                    $current_user      = wp_get_current_user();
                    if ( is_object( $current_user ) && !empty( $current_user->roles ) && is_array( $current_user->roles ) ) {
                        $match_role = array_intersect( $current_user->roles, $allowed_roles );
                        if ( count( $match_role ) == 0 )
                            $badge_html = '';
                    }
                }else{
                    $badge_html = '';
                }
            }
    
            return $badge_html;
        }
    
        add_filter( 'yith_wcbm_get_badge', 'yith_wcbm_customization_get_badge_by_user_role', 10, 3 );
    }

    About your other question, I invite you to contact us at https://yithemes.com

    • This reply was modified 6 years, 7 months ago by YITHEMES.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Restrict badge visibility by user role’ is closed to new replies.