php - 将特定用户角色的 WooCommerce 管理员订单限制为特定订单状态
问题描述
有没有办法显示特定角色的特定订单状态?例如 Admin 可以看到 On-Hold、Processing、Completed、Trash 等。但 Shop Manager 只能看到 On-Hold、Processing 和 Completed。
到目前为止,我已经尝试了在这个线程上找到的这段代码,但它仍然显示了商店经理的所有状态:
// Admin orders list: bulk order status change dropdown
add_filter( 'bulk_actions-edit-shop_order', 'filter_dropdown_bulk_actions_shop_order', 20, 1 );
function filter_dropdown_bulk_actions_shop_order( $actions ) {
$new_actions = [];
foreach( $actions as $key => $option ){
// Targeting "shop_manager" | order statuses "on-hold" and "processing"
if( current_user_can('shop_manager') && in_array( $key, array('mark_on-hold', 'mark_processing') ) ){
$new_actions[$key] = $option;
}
}
if( sizeof($new_actions) > 0 ) {
return $new_actions;
}
return $actions;
}
// Admin order pages: Order status change dropdown
add_filter('wc_order_statuses', 'filter_order_statuses');
function filter_order_statuses($order_statuses) {
global $pagenow;
if( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) {
$new_order_statuses = array();
foreach ($order_statuses as $key => $option ) {
// Targeting "shop_manager" | order statuses "on-hold" and "processing"
if( current_user_can('shop_manager') && in_array( $key, array('wc-on-hold', 'wc-processing') ) ){
$new_order_statuses[$key] = $option;
}
}
if( sizeof($new_order_statuses) > 0 ) {
return $new_order_statuses;
}
}
return $order_statuses;
}
解决方案
您确定要使用“current_user_can”吗?尝试 in_array('user_role") 指定为单个用户角色。
推荐阅读
- spring - Spring shell 与 JMX
- javascript - 使用 Chrome 分析 Web Worker 函数调用
- performance - Electron 默认整个代理
- c# - Visual Studio 2008 C# 项目 - 无法加载类型“System.Runtime.InteropServices.HandleRef”
- php - 从输入选定值中分离字符串
- python - 绘制 ax.twin 图时如何在 matplotlib 中将 x 轴刻度设置为日期
- c - C语言中不使用if..else语句求最大值
- c# - 从继承的类返回嵌套类
- c# - 使用字典时出现内存不足异常
- python - 以旧代码评估的格式字符串执行计算