Woocomerce | Wp-Admin Bar issue

Hello,

After uploading Woocommerce plugin to my website, Wp-admin bar dissapeared for all Buddypress logged in Users. So I made it appear again with the following code:

function me_wc_disable_admin_bar( $show_admin_bar ) {
if ( apply_filters( 'woocommerce_disable_admin_bar', get_option( 'woocommerce_lock_down_admin', 'yes' ) === 'yes' ) && ! ( current_user_can( 'edit_posts' ) || current_user_can( 'manage_woocommerce' ) ) ) {
$show_admin_bar = true;
}

return $show_admin_bar;
}
add_filter( 'show_admin_bar', 'me_wc_disable_admin_bar', 11, 2 );

Now the issue is that admin bar is visible to the visitors of website and the clients too. How could I exclude logged out users and customers from that code so they cannot have access to the admin bar?

Thank you!!

  • Adam Czajczyk

    Hello Ioannis,

    I hope you're well today and thank you for your question!

    I'm not sure how this happened as the default WooCommerce doesn't seem to be disabling toolbar and I wasn't able to replicate the issue on my test site in any way. I may be missing something though.

    I think however that this code should work:

    Give it a try please and let me know if it helped.

    function me_wc_disable_admin_bar( $show_admin_bar ) {
    if ( apply_filters( 'woocommerce_disable_admin_bar', get_option( 'woocommerce_lock_down_admin', 'yes' ) === 'yes' ) && ! ( current_user_can( 'edit_posts' ) || current_user_can( 'manage_woocommerce' ) ) ) {
    $show_admin_bar = true;
    }
    
    return $show_admin_bar;
    }
    
    function me_wc_check_if_enable_admin_bar() {
    if (is_user_logged_in()) {
    	add_filter( 'show_admin_bar', 'me_wc_disable_admin_bar', 11, 2 );
    }
    }
    add_action('wp_footer','me_wc_check_if_enable_admin_bar');

    Best regards,
    Adam

  • Ioannis

    Hello Adam,

    thank you so much for the code but unfortunatelly it didn't work in my case so here is what I did:

    I used the initial code I gave you in order to make the admin bar appear

    function me_wc_disable_admin_bar( $show_admin_bar ) {
    if ( apply_filters( 'woocommerce_disable_admin_bar', get_option( 'woocommerce_lock_down_admin', 'yes' ) === 'yes' ) && ! ( current_user_can( 'edit_posts' ) || current_user_can( 'manage_woocommerce' ) ) ) {
    $show_admin_bar = true;
    }
    
    return $show_admin_bar;
    }
    add_filter( 'show_admin_bar', 'me_wc_disable_admin_bar', 11, 2 );

    Then I added the following code in order to make the Admin Bar not to be visible to logged out users=visitors of the website

    add_filter( 'show_admin_bar', 'hide_admin_bar_from_visitor', 9999 );
    function hide_admin_bar_from_visitor() {
        if( ! is_user_logged_in() ){
            return false;
        }
        return true;
    }

    It worked for the visitors of the website. After that I added the last code once more time and I replaced the word visitors with the word customers but unfotunatelly it didn't work.. How could I modify it in order to make it work for customers too?

    Thank you,
    Ioannis

  • Adam Czajczyk

    Hello Ioannis!

    Thank you for your replay!

    It worked for the visitors of the website. After that I added the last code once more time and I replaced the word visitors with the word customers but unfotunatelly it didn't work.

    That last bit of code that you shared doesn't check anything other than whether to person viewing the site is logged in to the site or not. The main part here is this:

    if( ! is_user_logged_in() ){
            return false;
        }
        return true;

    - "is_user_logged_in()" function in a first line checks if "viewer" is logged in or not. If is logged in function returns boolean value of "true".
    - the "if" statement includes "!" which literally means "no", so the code inside is executed only if "is_user_logged_in()" returns "false" meaning that user is not logged in
    - in such case the "return false" is executed
    - otherwise this part is skipped and the "true" is returned

    By logic, this code should cause admin bar to be visible for logged in users and invisible for not logged in users. Which by the way should be the default behavior of WordPress.

    Furthermore, changing the word "visitor" to "customer" in your code doesn't affect anything because it's just a name of the function and you could use anything else there as long as it's consistent (the same in "add_filter()" and below in function name).

    That being said, I think both mine and your code won't work well here and I got a feeling that we're both missing something here and heading wrong direction.

    I think it would be best if we could start over and if I would be able to replicate the case on my setup. So, could you please give me some more information on this:

    After uploading Woocommerce plugin to my website, Wp-admin bar dissapeared for all Buddypress logged in Users.

    Some screenshots would also be helpful and if I could access and check your site via support access it would be extremely useful for me.

    Also, you mentioned "visitors" and "customer" - I understand that visitors are just visitors that are not logged in to your site but what/who do you mean by "customers"? Is it a separate user-role or all registered and logged in users of the site or just people that made a purchase?

    Please advise!
    Best regards,
    Adam

  • Ioannis

    Hello Adam,

    I have set multiple user roles in my website

    a) s2member levels that are all Service providers and
    b) Customers that have made a purchase or just have created an account ,can follow Service Providers and maybe make a purchase in the future

    So I would like the admin bar to appear for all the other roles when they are logged in except for the customers. I have already enabled Support Access and give you there the credentials to login as a client or as a service provider if you need to check the wp-admin bar visibility.

    You can find the above codes that I have added at the "Code Snippets" plugin in page 7. I hope that helps and to be easy to find a solution so the admin bar is not visible for customer's role.

    Thank you a lot,
    Ioannis

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.