You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何限制Woocommerce的Shop Manager权限:禁用订单页,仅允许添加商品分类

Hey there, let's tackle these two permission control needs for your WooCommerce store step by step—both are totally doable with custom code snippets (I’d suggest adding these to a custom plugin instead of your theme’s functions.php to avoid losing changes when you update your theme):

1. Restrict Orders Tab Access to Only Admins

First, we’ll hide the Orders tab from non-admin users and block direct access to the orders page if someone tries to navigate there manually. Add this code:

// Hide Orders tab from non-admins in WooCommerce admin
add_action('admin_menu', 'restrict_orders_tab_to_admin');
function restrict_orders_tab_to_admin() {
    if (!current_user_can('administrator')) {
        remove_menu_page('edit.php?post_type=shop_order');
    }
}

// Block direct access to orders page for non-admins
add_action('admin_init', 'block_direct_order_access');
function block_direct_order_access() {
    $current_page = isset($_GET['post_type']) ? $_GET['post_type'] : '';
    if ($current_page === 'shop_order' && !current_user_can('administrator')) {
        wp_redirect(admin_url());
        exit;
    }
}

This snippet first removes the Orders menu item for anyone who isn’t an admin. Then it checks if someone is trying to access the orders page directly (via URL) and redirects them to the main admin dashboard if they’re not authorized.

2. Limit Shop Manager to Only Add Products and Categories

By default, Shop Managers have a lot of product-related permissions—we need to strip those back so they can only add new products and categories, not edit or delete existing ones. Here’s how:

// Adjust Shop Manager permissions to only add products and categories
add_action('init', 'restrict_shop_manager_permissions');
function restrict_shop_manager_permissions() {
    // Get the Shop Manager role
    $shop_manager = get_role('shop_manager');

    // Remove unwanted permissions
    // Remove product edit/delete permissions
    $shop_manager->remove_cap('edit_products');
    $shop_manager->remove_cap('edit_published_products');
    $shop_manager->remove_cap('delete_products');
    $shop_manager->remove_cap('delete_published_products');
    // Remove category edit/delete permissions
    $shop_manager->remove_cap('edit_product_terms');
    $shop_manager->remove_cap('delete_product_terms');

    // Keep only the permissions needed to add products and categories
    $shop_manager->add_cap('publish_products'); // Allow adding new products
    $shop_manager->add_cap('manage_product_terms'); // Allow creating new categories
    $shop_manager->add_cap('assign_product_terms'); // Allow assigning categories to new products
}

A quick breakdown:

  • We first grab the shop_manager role object
  • We remove permissions that let them edit or delete existing products/categories
  • We keep only the permissions required to publish new products, create new categories, and assign those categories to the products they add

Note: After adding this code, test it thoroughly with a Shop Manager account to make sure they can’t access edit/delete functions for existing items. If you have any third-party plugins that add extra product-related permissions, you might need to adjust the snippet to remove those too.

内容的提问来源于stack exchange,提问作者Sabhay Sardana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:42:47