仅为特定用户禁用产品数据部分

问题描述 投票:3回答:1

在WooCommerce后端,我知道您可以使用functions.php上的一些代码全局删除“产品”选项卡。

但我只想为用户后端删除。我正在使用多厂商插件。

我该怎么做?

我的代码:

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);

谢谢。

php wordpress tabs woocommerce product
1个回答
2
投票

假设您的供应商具有自定义用户角色,您可以通过以下方式在您的函数中实现此特定用户角色:

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);
}

此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中。

此代码经过测试和运行。

© www.soinside.com 2019 - 2024. All rights reserved.