Search code examples
phpwordpresswoocommercesettingshook-woocommerce

Remove specific tabs in from WooCommerce admin settings


I would like specific users to only see the WooCommerce -> Settings -> Shipping menu. I've managed to remove other tabs e.g. Products, Payments, etc, but stuck on the following 2 things that I want to accomplish:

  1. Remove the "GENERAL" tab in WooCommerce Settings.
  2. Remove the "ORDER STATUSES" Tab from a plugin. (Note: this isn't a tab exactly, the slug is 'edit.php?post_type=wc_order_status')

enter image description here

When I try to remove the GENERAL tab, it eliminates the entire Settings menu. As for the 'Order Statuses', my code just doesn't work.

add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $array ) {

    global $current_user;

    //Declare the tabs we want to hide
    $tabs_to_hide = array(
        'general'         => 'General', //this one removes the entire Settings menu
        'wc_order_status' => 'Order Statuses'// this doesn't work, maybe bc it's a post_type
        );

    // Remove tab if user role is shipping_manager
    if ( in_array("shipping_manager", $$current_user->roles) ) {
        $array = array_diff_key($array, $tabs_to_hide);
    }
}

I had also tried the below code to remove the ORDER STATUSES tab, but still no luck:

    add_action( 'admin_menu', 'remove_order_statuses_tab', 999);
    function remove_order_statuses_tab() 
    {
      global $current_user;

      if ( in_array("shipping_manager", $current_user->roles) ) {
            remove_menu_page( 'edit.php?post_type=wc_order_status' ); //not working either
         }

    }

Solution

  • The correct hook to be used is woocommerce_settings_tabs_array.

    First you need to find which are the array keys that you need to remove from the tabs array in your code.

    For that you will use first the following function that will display all array data in Admin WooCommerce settings (only for testing, to be removed):

    add_filter( 'woocommerce_settings_tabs_array', 'filter_wc_settings_tabs_array', 990, 1 );
    function filter_wc_settings_tabs_array( $tabs_array ) {
        // Display raw array data
        echo '<pre>'; print_r( $tabs_array ); echo '</pre>';
    
        return $tabs_array;
    }
    

    It will display something like (with all required array keys):

    enter image description here

    Now you can get the array keys slugs that you need, to remove the corresponding tab settings. So now you will be able to find the correct array key slug for you Third party plugin WooCommerce setting tab, that you will use in the function code below.

    To target a specific user role, you can either use global $current_user; $current_user->roles; or the Wordpress dedicated function current_user_can()

    So the working code that will remove specific setting tabs for a user role is:

    add_filter( 'woocommerce_settings_tabs_array', 'filter_wc_settings_tabs_array', 200, 1 );
    function filter_wc_settings_tabs_array( $tabs_array ) {
        // Only for "shipping_manager" user role
        if( current_user_can( 'shipping_manager' ) ) {
            // Remove some specific tabs
            unset( $tabs_array['general'], $tabs_array['order_status'] ); // <== replace 'order_status' by 
        }
    
        return $tabs_array;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.