如何针对店铺管理员移除WooCommerce订单操作中的「移至回收站」按钮?
解决WooCommerce订单中针对Shop Manager隐藏「移至回收站」按钮的问题
Hey there! Let's break this down clearly for you:
First, the code you shared won't work for WooCommerce orders—here's why:
- It targets a custom post type named
inhoud, but WooCommerce orders use theshop_orderpost type - There’s no role-based check, so it would hide the button for all backend users, not just Shop Managers
Luckily, modifying it to fit your needs is straightforward. Below is the adjusted code that will only hide the "Move to Trash" button for Shop Managers on WooCommerce order pages:
function hide_order_trash_button_for_shop_manager() { // Grab the current logged-in user $current_user = wp_get_current_user(); // Check if the user is a Shop Manager AND we're on an order page if ( in_array( 'shop_manager', $current_user->roles ) && get_current_screen()->post_type === 'shop_order' ) { ?> <style type="text/css"> /* Hide the trash button on individual order edit pages */ form #delete-action { display: none !important; } /* Optional: Hide bulk trash option on the orders list page too */ .post-type-shop_order .bulkactions #bulk-action-selector-top option[value="trash"], .post-type-shop_order .bulkactions #bulk-action-selector-bottom option[value="trash"] { display: none !important; } </style> <?php } } add_action( 'admin_head', 'hide_order_trash_button_for_shop_manager' );
What this code does:
- Role verification: Uses
wp_get_current_user()to confirm the user has theshop_managerrole - Page targeting: Ensures the custom styles only load on WooCommerce order pages (where the post type is
shop_order) - Style adjustments:
- The first CSS rule hides the standalone "Move to Trash" button on single order edit pages
- The optional second rule blocks the bulk "Move to Trash" option on the main orders list—feel free to remove this if you don’t need to restrict bulk actions
Quick tips:
- Add this code to your theme's
functions.phpfile, or use a custom code snippet plugin (like Code Snippets) to avoid losing changes when your theme updates - This selector works with the latest WooCommerce versions, but if WooCommerce updates their backend UI structure later, you might need to tweak the CSS selectors slightly
内容的提问来源于stack exchange,提问作者Cole




