WooCommerce:仅针对店铺经理实现订单页"Order created"与客户邮箱只读及禁用编辑权限的解决方案问询
Solution for Restricting Shop Managers from Editing Order Date and Customer in WooCommerce
Got it, let's fix this specifically for Shop Managers. The issue with your previous plugin attempt might be that it wasn't targeting the right user role or the correct DOM elements. Below is a complete code snippet you can add to your theme's functions.php file (or better yet, a custom plugin to avoid losing changes when your theme updates):
1. Backend Protection (Prevent Unauthorized Form Submissions)
This part stops Shop Managers from saving changes to the order date and customer, even if they bypass the frontend UI restrictions:
add_action('woocommerce_process_shop_order_meta', 'lock_order_date_and_customer_for_shop_managers', 10, 2); function lock_order_date_and_customer_for_shop_managers($order_id, $post) { // Check if current user is a Shop Manager (exclude admins) if (current_user_can('manage_woocommerce') && !current_user_can('administrator')) { // Get the original order object $order = wc_get_order($order_id); // Reset order date to its original value to block changes update_post_meta($order_id, '_order_date', $order->get_date_created()->format('Y-m-d H:i:s')); // Reset customer ID to its original value to block changes update_post_meta($order_id, '_customer_user', $order->get_customer_id()); } }
2. Frontend UI Restrictions (Make Fields Readonly & Hide Edit Buttons)
This part updates the order edit page to make the fields visually readonly and removes the edit controls:
add_action('admin_enqueue_scripts', 'enforce_readonly_order_fields_for_shop_managers'); function enforce_readonly_order_fields_for_shop_managers() { // Only target the WooCommerce order edit page if (get_current_screen()->id !== 'shop_order') return; // Check if current user is a Shop Manager (exclude admins) if (current_user_can('manage_woocommerce') && !current_user_can('administrator')) { wp_add_inline_script('jquery', ' jQuery(document).ready(function($) { // Lock the "Order created" date field and hide its edit button const orderDateField = $("#order_date"); orderDateField.prop("readonly", true); orderDateField.siblings(".edit").hide(); // Lock the "Customer" selection field and hide its edit button const customerField = $("#customer_user"); customerField.prop("readonly", true); customerField.siblings(".edit").hide(); // Lock the customer email input field if it exists const customerEmailField = $("#email"); if (customerEmailField.length) { customerEmailField.prop("readonly", true); } }); '); } }
Key Notes:
- Role Targeting: The code uses
manage_woocommerce(a capability Shop Managers have) and excludes admins with!current_user_can('administrator')to ensure only Shop Managers are restricted. - Safety First: Always back up your
functions.phpbefore adding code, or use a simple custom plugin (create a PHP file with this code plus a basic plugin header) to avoid losing changes when updating your theme. - Why Your Previous Plugin Failed: Tools like Simple Custom CSS and JS often load scripts too early, or lack the necessary role checks, leading to code that doesn't target the right users or elements. This solution hooks directly into WordPress and WooCommerce admin actions to run at the correct time.
内容的提问来源于stack exchange,提问作者Cole
相关产品推荐
相关产品推荐

