如何为WooCommerce后台订单中的商品链接设置Shop Manager不可点击或跳转至无权限页面?
Hey there! I get exactly what you need—locking down Shop Managers from accessing product edit pages via order items in the WooCommerce backend, since you only want them viewing order details, not modifying products. The code you tried before was frontend-focused, so let’s fix that with a backend-specific solution that fits right into your functions.php file.
Option 1: Turn Product Names into Unclickable Plain Text
This snippet will strip the anchor tag around product names in backend orders only for Shop Managers, turning clickable links into static text:
add_filter( 'woocommerce_order_item_name', 'disable_order_item_links_for_shop_managers', 10, 3 ); function disable_order_item_links_for_shop_managers( $item_name, $item, $is_visible ) { // Only target the WordPress admin area if ( ! is_admin() ) return $item_name; // Check if the current user is a Shop Manager $current_user = wp_get_current_user(); if ( in_array( 'shop_manager', $current_user->roles ) ) { // Return just the raw product name without any link return $item->get_name(); } // Leave the original link intact for other user roles return $item_name; }
Option 2: Redirect to a "No Permission" Page
If you’d rather keep the link but send Shop Managers to a custom "no access" page instead of the product edit screen, use this version. Just replace YOUR_NO_ACCESS_PAGE_ID with the ID of your dedicated no-permission page:
add_filter( 'woocommerce_order_item_name', 'redirect_order_item_links_for_shop_managers', 10, 3 ); function redirect_order_item_links_for_shop_managers( $item_name, $item, $is_visible ) { if ( ! is_admin() ) return $item_name; $current_user = wp_get_current_user(); if ( in_array( 'shop_manager', $current_user->roles ) ) { // Get the URL of your "No Access" page $no_access_url = get_permalink( YOUR_NO_ACCESS_PAGE_ID ); // Swap the original product edit link with your no-access page URL return str_replace( get_edit_post_link( $item->get_product_id() ), $no_access_url, $item_name ); } return $item_name; }
How to Implement This
- Add either snippet to your child theme’s
functions.phpfile (never edit the parent theme directly—you’ll lose changes when the theme updates!) - If you’re not comfortable editing theme files, use a custom code snippets plugin instead.
Why This Works
We’re hooking into the woocommerce_order_item_name filter, which controls how product names are rendered in order items. First, we confirm we’re in the admin area, then check the user’s role. For Shop Managers, we either remove the link entirely or replace it with your no-access page URL—no more accidental product edits from your team!
内容的提问来源于stack exchange,提问作者Cole

