hide a woocommerce setting tab

2020-07-22 18:00发布

问题:

I would like to hide a specific woocommerce setting tab by user role. Not the entire submenu, but just a tab(checkout to be specific). I want shop managers to be able to access most of the settings, but be unable to affect the checkout settings.

How can I achieve this?

回答1:

If you want to remove the tabs instead of hiding them using CSS, then you can add the following to yours theme functions.php:

add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $tabs ) {
    // Declare the tabs we want to hide
    $tabs_to_hide = array(
        'Tax',
        'Checkout',
        'Emails',
        'API',
        'Accounts',
        );


    // Get the current user
    $user = wp_get_current_user();

    // Check if user is a shop-manager
    if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {

        // Remove the tabs we want to hide
        $tabs = array_diff($tabs, $tabs_to_hide);
    }

    return $tabs;
}

This uses the WooCommerce 'woocommerce_settings_tabs_array' filter. For more information on all the WooCommerce filters and hooks you can look here: https://docs.woocommerce.com/wc-apidocs/hook-docs.html

This just has the added benefit that it is no longer in the HTML, so if anyone looks at the source, they won't find the elements.

You can still access the URLs. This is just a way of removing the tabs instead of hiding them.

EDIT: I've figured out how to stop access to the URLs. Copy the following:

add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $array ) {
    // Declare the tabs we want to hide
    $tabs_to_hide = array(
        'tax' => 'Tax',
        'checkout' => 'Checkout',
        'email' => 'Emails',
        'api' => 'API',
        'account' => 'Accounts',
        );

    // Get the current user
    $user = wp_get_current_user();

    // Check if user is a shop_manager
    if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {

        // Remove the tabs we want to hide from the array
        $array = array_diff_key($array, $tabs_to_hide);

        // Loop through the tabs we want to remove and hook into their settings action
        foreach($tabs_to_hide as $tabs => $tab_title) {
            add_action( 'woocommerce_settings_' . $tabs , 'redirect_from_tab_page');
        }
    }

    return $array;
}

function redirect_from_tab_page() {
    // Get the Admin URL and then redirect to it
    $admin_url = get_admin_url();
    wp_redirect($admin_url);
    exit;
}

This is pretty much the same as the first bit of code, apart from the array is structured differently and I've added a foreach. The foreach goes through the list of tabs we want to block, hooks into the 'woocommerce_settings_{$tab}' action which is used to show the settings pages.

Then I created a redirect_from_tab_page function to redirect the users to the default admin URL. This stops direct access to the different settings tabs.



回答2:

Put this code in your theme/child theme functions.php or somewhere else:

if (!function_exists('hide_setting_checkout_for_shop_manager')){
    function hide_setting_checkout_for_shop_manager() {

        $user = wp_get_current_user();
        //check if user is shop_manager
        if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
            echo '<style> .woocommerce_page_wc-settings  form .woo-nav-tab-wrapper a[href="'.admin_url('admin.php?page=wc-settings&tab=checkout').'"]{ display: none; } </style>';
        }

    }
}
add_action('admin_head', 'hide_setting_checkout_for_shop_manager');

The style will be output to html head only at wp-admin and the login user role is shop_manager.

For more about admin_head hook, please check https://codex.wordpress.org/Plugin_API/Action_Reference/admin_head