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

WooCommerce中仅针对店铺管理员角色限制订单编辑权限的实现问题

WooCommerce Shop Manager角色订单只读权限定制方案

1. 仅对shop manager角色禁用添加订单功能

修改原代码增加角色判断逻辑,仅对店铺管理员生效,管理员权限不受影响:

add_filter( 'woocommerce_register_post_type_shop_order','restrict_shop_manager_order_create' );
function restrict_shop_manager_order_create($fields) {
    $current_user = wp_get_current_user();
    // 仅匹配shop manager角色,排除管理员
    if ( in_array( 'shop_manager', (array) $current_user->roles ) && !in_array( 'administrator', (array) $current_user->roles ) ) {
        $fields['capabilities'] = array(
            'create_posts' => false,
        );
    }
    return $fields;
}

将上述代码添加到主题functions.php文件或自定义代码片段插件中即可生效。

2. 禁用订单创建日期编辑+剩余只读需求统一实现方案

原代码失效通常是两个原因:未做角色权限判断、脚本加载路径错误、选择器匹配不准。以下整合了所有你提的只读需求,所有控制仅对shop manager角色生效,管理员功能完全不受影响:

add_action( 'admin_enqueue_scripts', 'enqueue_shop_manager_order_readonly_script' );
function enqueue_shop_manager_order_readonly_script( $hook ) {
    // 仅在订单相关页面加载脚本
    if ( ( $hook == 'post.php' || $hook == 'edit.php' ) && get_post_type() == 'shop_order' ) {
        $current_user = wp_get_current_user();
        // 仅给shop manager加载只读控制脚本
        if ( in_array( 'shop_manager', (array) $current_user->roles ) && !in_array( 'administrator', (array) $current_user->roles ) ) {
            add_action( 'admin_footer', 'shop_manager_order_readonly_js' );
        }
    }
}

function shop_manager_order_readonly_js() {
?>
<script>
jQuery(document).ready(function(){
    // 2. 禁用创建日期编辑
    jQuery(".order_data_column_container .order_data_column input[name='order_date']").attr('disabled', true);
    
    // 3. 禁用客户信息编辑
    jQuery('.order_data_column input, .order_data_column select, .edit_address').attr('disabled', true);
    jQuery('.edit_address').hide();
    
    // 4. 禁用商品跳转链接,保留文本和SKU展示
    jQuery('.wc-order-item-name a').each(function(){
        jQuery(this).replaceWith(jQuery(this).text());
    });
    
    // 5. 禁用订单操作功能
    jQuery('#wc-order-actions select, #wc-order-actions button').attr('disabled', true);
    
    // 6. 禁用自定义字段编辑
    jQuery('#postcustom .postbox input, #postcustom .postbox select, #postcustom .postbox textarea').attr('disabled', true);
    
    // 7. 禁用添加新自定义字段功能
    jQuery('#postcustom #newmeta-submit, #postcustom .add-meta').hide();
    
    // 8. 禁用下载权限设置功能
    jQuery('#woocommerce-order-downloads').hide();
});
</script>
<?php
}

将上述完整代码添加到主题functions.php文件即可实现所有需求,无需额外修改其他文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 10:06:03