Disable product data section for specific users on

2020-04-11 05:45发布

问题:

In WooCommerce backend, I know that you can remove the Product tabs globally with some code on functions.php.

But I only want to remove for the user back end. I'm using a multivendor plugin.

How do I do it?

My code:

function remove_tab($tabs){
    unset($tabs['inventory']); // it is to remove inventory tab
    //unset($tabs['advanced']); // it is to remove advanced tab
    //unset($tabs['linked_product']); // it is to remove linked_product tab
    //unset($tabs['attribute']); // it is to remove attribute tab
    //unset($tabs['variations']); // it is to remove variations tab
    return($tabs);
}
add_filter('woocommerce_product_data_tabs', 'remove_tab', 10, 1);

Thanks.

回答1:

Supposing that your vendors have a custom user role, you can achieve this targeting this specific user role in your function, this way:

add_filter('woocommerce_product_data_tabs', 'verdors_remove_tab', 10, 1);
function verdors_remove_tab($tabs){

    // Set HERE your targeted user role SLUG
    $target_user_role = 'multivendor';

    // Get current user (object)
    $current_user = wp_get_current_user();
    $current_user_roles = $current_user->roles; // current user roles

    // Unsetting tabs for this specific user role
    if( in_array( $target_user_role, $current_user_roles ) ){
        unset($tabs['inventory']); // it is to remove inventory tab
        //unset($tabs['advanced']); // it is to remove advanced tab
        //unset($tabs['linked_product']); // it is to remove linked_product tab
        //unset($tabs['attribute']); // it is to remove attribute tab
        //unset($tabs['variations']); // it is to remove variations tab
    }
    return($tabs);
}

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.