0
votes

How can I restrict users with a certain role(shop manager) from accessing selected woocommerce settings tabs. I asked a similar question here, but the answer will only hide the tab from showing up but will not restrict the user from the page if he types the url directly.

I can't seem to figure this out.

1

1 Answers

1
votes

This is not very pretty but that will work :

add_filter( 'woocommerce_settings_tabs_array','remove_setting_tab', 50 );

function remove_setting_tab( $settings_tabs ) {
    //Default Tabs are : 
    //array(8) { ["general"]=> string(7) "General" ["products"]=> string(8) "Products" ["tax"]=> string(3) "Tax" ["checkout"]=> string(8) "Checkout" ["shipping"]=> string(8) "Shipping" ["account"]=> string(8) "Accounts" ["email"]=> string(6) "Emails" ["api"]=> string(3) "API" }      
    $user = wp_get_current_user();
    if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
        unset($settings_tabs['general']);// = array();
    }

    return $settings_tabs;
}

add_filter( 'woocommerce_general_settings', 'setting_tab_empty_content' );
function setting_tab_empty_content($settings){
    $user = wp_get_current_user();
    if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
        if ($_SERVER['REQUEST_URI']=='/wp-admin/admin.php?page=wc-settings&tab=general')
        {
             die( '<strong>No permissions</strong>' );
        }
    }
}