【问题标题】:Restrict WooCommerce admin orders for specific user roles to specific order statusses将特定用户角色的 WooCommerce 管理员订单限制为特定订单状态
【发布时间】:2021-07-22 19:54:33
【问题描述】:

有没有办法显示特定角色的特定订单状态?例如。管理员可以看到 On-Hold、Processing、Completed、Trash 等。但是 Shop Manager 只能看到 On-Hold、Processing 和 Completed。

见截图:

到目前为止,我已经尝试了在此线程 here 上找到的代码,但它仍然显示商店经理的所有状态:

// 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;
}

【问题讨论】:

    标签: php wordpress woocommerce orders user-roles


    【解决方案1】:

    您确定要使用“current_user_can”吗?尝试 in_array('user_role") 指定为单个用户角色。

    【讨论】:

    • 感谢您的建议。会试试的。
    【解决方案2】:

    我以更好的方式重新访问了您的代码,并添加了缺失的功能,该功能可根据管理订单列表中特定用户角色的允许订单状态过滤订单:

    // Custom conditional fuction to target specific user roles
    function user_roles_allowed_orders() {
        $targeted_roles = array('shop_manager'); // Here define your targeted user roles
        return (bool) array_intersect( wp_get_current_user()->roles, $targeted_roles );
    }
    
    // Admin orders list: bulk order status change dropdown
    add_filter( 'bulk_actions-edit-shop_order', 'filter_dropdown_bulk_actions_shop_order', 100 );
    function filter_dropdown_bulk_actions_shop_order( $actions ) {
        if ( user_roles_allowed_orders() ) {
            $allowed_actions = array('mark_on-hold', 'mark_processing');
    
            foreach( $actions as $key => $option ){
                if( ! in_array( $key, $allowed_actions ) ){
                    unset($actions[$key]);
                }
            }
        }
        return $actions;
    }
    
    // Admin order pages: Order status change dropdown
    add_filter('wc_order_statuses', 'filter_order_statuses', 100 );
    function filter_order_statuses( $statuses ) {
        global $pagenow, $typenow;
    
        if( in_array( $pagenow, array('post.php', 'post-new.php') )
        && 'shop_order' === $typenow && user_roles_allowed_orders() ) {
            $allowed_statusses = array('wc-on-hold', 'wc-processing');
    
            foreach ($statuses as $key => $option ) {
                if( ! in_array( $key, $allowed_statusses ) ){
                    unset($statuses[$key]);
                }
            }
        }
        return $statuses;
    }
    
    // Filter admin orders for shop managers based
    add_action( 'pre_get_posts', 'filter_shop_manager_orders', 100 );
    function filter_shop_manager_orders( $query ) {
        global $pagenow, $post_type;
        //'shop_manager'
        if( $query->is_admin && 'edit.php' === $pagenow && 'shop_order' === $post_type
        && user_roles_allowed_orders() ){
            $allowed_statusses = array('wc-on-hold', 'wc-processing');
    
            $query->set( 'post_status', $allowed_statusses );
        }
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 嗨 Loic,非常感谢。有用。现在,我想知道如何仅为选定角色隐藏这些订单状态标签?例如。如果商店经理转到 woocommerce 订单后端页面,他们只会看到保留和处理标签,而不是上面屏幕截图中列出的所有标签(我的、报价、发票、待处理等)。这可以使用 CSS 完成吗?编辑:添加更多 cmets。
    猜你喜欢
    • 2018-03-04
    • 2020-09-01
    • 1970-01-01
    • 2019-07-20
    • 2019-08-23
    • 2018-12-24
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    相关资源
    最近更新 更多