Search code examples
phpwordpresswoocommerceadminuser-roles

Hide WooCommerce admin submenu "Home" from shop manager


I would like to hide a submenu 'home' in WooCommerce from shop manager.

I can hide/disable the wc-admin with the following code:

function AtoJ_remove_menus() {
    if (current_user_can( 'shop_manager' )) {
        // WooCommerce
        remove_submenu_page( 'woocommerce', 'wc-admin' );
    }
}
add_action( 'admin_menu', 'AtoJ_remove_menus', 999 );

But if I do it that way, it also disables the permission to access the WooCommerce Analytics admin section for shop manager.

enter image description here


Solution

  • To disable the Admin WooCommerce "Home" page (only) for specific user role, we can hide WooCommerce "Home" submenu item and redirect the related request to WooCommerce "Orders" page:

    add_action( 'admin_head', 'disable_admin_woocommerce_home_fur_css' );
    function disable_admin_woocommerce_home_fur_css() {
        $targeted_role = 'administrator'; // Here define the user role
        if ( current_user_can( $targeted_role ) ) :
        ?><style> li.toplevel_page_woocommerce ul li.wp-first-item {display:none !important;}</style><?php
        endif;
    }
    
    
    add_action( 'admin_menu', 'disable_admin_woocommerce_home_for_user_role', 999 );
    function disable_admin_woocommerce_home_for_user_role() {
        global $submenu;
        $targeted_role = 'administrator'; // Here define the user role
        if ( current_user_can( $targeted_role ) ) {
            if ( isset($_GET['page']) && $_GET['page'] == 'wc-admin' && ! isset($_GET['path']) ) {
                wp_redirect( admin_url('edit.php?post_type=shop_order') );
                exit();
            }
        }
    }
    

    It will allow shop manager to access Analytics section.

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