如何仅在筛选处理中订单时显示后台订单列表批量操作?
仅在处理状态订单列表显示批量操作的实现方案
完全可行,你可以通过判断当前订单列表的筛选状态,结合WooCommerce的钩子来实现需求。以下是具体代码:
add_filter( 'bulk_actions-edit-shop_order', 'custom_restrict_bulk_actions_to_processing_orders' ); function custom_restrict_bulk_actions_to_processing_orders( $actions ) { // 仅在订单列表页面执行逻辑 if ( ! isset( $_GET['post_type'] ) || $_GET['post_type'] !== 'shop_order' ) { return $actions; } // 获取当前筛选的订单状态 $current_status = isset( $_GET['post_status'] ) ? $_GET['post_status'] : ''; // 若当前不是处理状态(wc-processing),移除所有批量操作 if ( $current_status !== 'wc-processing' ) { return array(); } // 处理状态下保留原有批量操作 return $actions; }
代码说明:
- 借助
bulk_actions-edit-shop_order钩子修改订单列表的批量操作选项 - 先验证当前页面是否为订单列表,避免影响后台其他页面功能
- 通过
$_GET['post_status']获取用户当前筛选的订单状态 - 仅当筛选状态为
wc-processing时保留批量操作,其他状态下清空操作列表
将上述代码添加到你的主题functions.php文件或自定义插件中即可生效。
内容的提问来源于stack exchange,提问作者David
相关产品推荐
相关产品推荐

