php - 限制用户角色以仅更改 Woocommerce 中的某些订单状态
问题描述
我想从 woocommerce 的下拉列表中限制按角色访问某些订单状态。我已经在functions.php子主题中尝试了按角色限制woocommerce订单状态的代码,但无法使其正常工作并且没有足够的代表发表评论。
https://prnt.sc/mpfl3b是所显示内容的屏幕截图 - 我希望商店经理(或创建的自定义角色)只能将订单标记为处理中或暂停,而从落下。
任何建议将被认真考虑。
解决方案
链接的答案代码不适用于您想要的。而是尝试以下操作:
// 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;
}
代码位于您的活动子主题(或活动主题)的 function.php 文件中。它应该有效。
推荐阅读
- php - 在 php 表单中添加输入文本字段并将数据存储在文件中也显示动态表中的文本
- javascript - 如何使用 cy.intercept 存根两个同名请求但返回两个不同的主体?
- c++ - 同步访问两个矩阵
- python - 我声明替换成员不和谐机器人
- swift - 网址组件没有快速显示正确的网址
- c++ - Need example of conditional operator yielding an lvalue using conversion
- javascript - Testing a component with Jest Error in render: "TypeError: Cannot read property 'template' of null"
- python - xarray.Dataset.to_zarr:如果存在,则使用 append_dim 覆盖数据
- javascript - Throwing errors with socket.io
- sql-server - 如何在不使用 SQL Server 中的 Top、CTE、RowNum 和 Rank 的情况下从表中获取最频繁的值?