从WooCommerce管理员设置中删除特定选项卡

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

我希望特定用户只看到WooCommerce->设置->运送菜单。我设法删除了其他标签,例如产品,付款等,但停留在我要完成的以下两件事上:

  1. 删除WooCommerce设置中的“常规”标签。
  2. 从插件中删除“订单状态”标签。 (请注意:这并非完全是标签页,'edit.php?post_type = wc_order_status')

enter image description here

当我尝试删除“常规”选项卡时,它会消除整个“设置”菜单。至于“订单状态”,我的代码不起作用。

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

我也尝试过下面的代码来删除“ ORDER STATUSES”选项卡,但仍然没有运气:

    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
         }

    }
php wordpress woocommerce settings hook-woocommerce
2个回答
0
投票
add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );

function remove_woocommerce_setting_tabs( $array ) {

        global $current_user;

        // Remove tab if user role is shipping_manager

        if ( in_array( "shipping_manager", $current_user->roles ) ) {
            unset( $array[ 'general' ] );
            ?>
                <script>
            document.querySelector("[href='<?php echo esc_url( admin_url( 'edit.php?post_type=wc_order_status' ) ); ?>']").style.display = 'none';

            </script>
        <?php
    }
    return $array;
}

尝试此代码段


0
投票

要使用的正确钩子是woocommerce_settings_tabs_array

首先,您需要找到要从代码中的tabs数组中删除的array keys

为此,您将首先使用以下功能,该功能将在Admin WooCommerce设置中显示所有阵列数据((仅用于测试,将被删除)

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

它将显示类似于((具有所有必需的数组键)

enter image description here

现在您可以获取所需的阵列键块,以删除相应的选项卡设置。因此,现在您将能够为您找到正确的数组键段]第三方插件WooCommerce设置选项卡,将在下面的功能代码中使用。

以特定的用户角色为目标,您可以使用全局$ current_user; $ current_user->角色;或Wordpress专用功能current_user_can()

因此将删除用户角色的特定设置选项卡的工作代码是:

current_user_can()

代码进入您的活动子主题(或活动主题)的functions.php文件中。经过测试和工作。

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