隐藏 woocommerce 设置选项卡 [英] hide a woocommerce setting tab

查看:39
本文介绍了隐藏 woocommerce 设置选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按用户角色隐藏特定的 woocommerce 设置选项卡.不是整个子菜单,而只是一个选项卡(要具体结帐).我希望商店经理能够访问大部分设置,但无法影响结帐设置.

I would like to hide a specific woocommerce setting tab by user role. Not the entire submenu, but just a tab(checkout to be specific). I want shop managers to be able to access most of the settings, but be unable to affect the checkout settings.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

如果您想删除选项卡而不是使用 CSS 隐藏它们,那么您可以在您的主题 functions.php 中添加以下内容:

If you want to remove the tabs instead of hiding them using CSS, then you can add the following to yours theme functions.php:

add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $tabs ) {
    // Declare the tabs we want to hide
    $tabs_to_hide = array(
        'Tax',
        'Checkout',
        'Emails',
        'API',
        'Accounts',
        );


    // Get the current user
    $user = wp_get_current_user();

    // Check if user is a shop-manager
    if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {

        // Remove the tabs we want to hide
        $tabs = array_diff($tabs, $tabs_to_hide);
    }

    return $tabs;
}

这使用 WooCommerce 'woocommerce_settings_tabs_array' 过滤器.有关所有 WooCommerce 过滤器和挂钩的更多信息,您可以在此处查看:https://docs.woocommerce.com/wc-apidocs/hook-docs.html

This uses the WooCommerce 'woocommerce_settings_tabs_array' filter. For more information on all the WooCommerce filters and hooks you can look here: https://docs.woocommerce.com/wc-apidocs/hook-docs.html

这只是一个额外的好处,它不再在 HTML 中,所以如果有人查看源代码,他们将找不到元素.

This just has the added benefit that it is no longer in the HTML, so if anyone looks at the source, they won't find the elements.

您仍然可以访问这些 URL.这只是移除选项卡而不是隐藏它们的一种方式.

You can still access the URLs. This is just a way of removing the tabs instead of hiding them.

我已经找到了如何停止访问这些 URL 的方法.复制以下内容:

I've figured out how to stop access to the URLs. Copy the following:

add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $array ) {
    // Declare the tabs we want to hide
    $tabs_to_hide = array(
        'tax' => 'Tax',
        'checkout' => 'Checkout',
        'email' => 'Emails',
        'api' => 'API',
        'account' => 'Accounts',
        );

    // Get the current user
    $user = wp_get_current_user();

    // Check if user is a shop_manager
    if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {

        // Remove the tabs we want to hide from the array
        $array = array_diff_key($array, $tabs_to_hide);

        // Loop through the tabs we want to remove and hook into their settings action
        foreach($tabs_to_hide as $tabs => $tab_title) {
            add_action( 'woocommerce_settings_' . $tabs , 'redirect_from_tab_page');
        }
    }

    return $array;
}

function redirect_from_tab_page() {
    // Get the Admin URL and then redirect to it
    $admin_url = get_admin_url();
    wp_redirect($admin_url);
    exit;
}

这与第一部分代码几乎相同,除了数组的结构不同并且我添加了一个 foreach.foreach 遍历我们要阻止的选项卡列表,挂钩到用于显示设置页面的woocommerce_settings_{$tab}"操作.

This is pretty much the same as the first bit of code, apart from the array is structured differently and I've added a foreach. The foreach goes through the list of tabs we want to block, hooks into the 'woocommerce_settings_{$tab}' action which is used to show the settings pages.

然后我创建了一个 redirect_from_tab_page 函数来将用户重定向到默认的管理 URL.这将停止直接访问不同的设置选项卡.

Then I created a redirect_from_tab_page function to redirect the users to the default admin URL. This stops direct access to the different settings tabs.

这篇关于隐藏 woocommerce 设置选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆