首页 > 解决方案 > 用户角色编辑器:仅显示具有选定状态的角色 WooCommerce 订单

问题描述

我使用代码仅查看具有选定状态的订单

这是代码


add_filter('ure_role_additional_options', 'add_show_processing_orders_only_to_admin_option', 10, 1);

function add_show_processing_orders_only_to_admin_option($items) {
    $item = URE_Role_Additional_Options::create_item('show_processing_orders_only', esc_html__('Show orders with =Processing= status only', 'user-role-editor'), 'init', 'show_processing_orders_only');
    $items[$item->id] = $item;

    return $items;
}

function show_processing_orders_only() {

    new URE_WC_Order_Status_Restrictor();

}


class URE_WC_Order_Status_Restrictor {

    private $allowed_statuses = array(
        'wc-processing',    // Processing
        //'wc-cancelled'      // Cancelled
    );
    private $not_allowed_statuses = array(
        'all',           // All
        'wc-pending',    // Pending payment
        'wc-on-hold',    // On hold
        'wc-completed',  // Completed
        'wc-refunded',   // Refunded
        'wc-failed'      // Failed
    );

    function __construct() {

        add_filter( 'views_edit-shop_order', array($this, 'remove_views'), 10, 1 );
        add_action('pre_get_posts', array($this, 'filter_status'), 10, 1);
        add_action('posts_selection', array($this, 'block_status'));

    }


    private function apply_restrictions() {
        if (!is_admin()) {
            return false;
        }

        $user = wp_get_current_user();
        if (!empty($user) && !empty($user->roles) && in_array('administrator', $user->roles)) {
            return false;
        }

        return true;
    }
    // end of apply_restrictions()


    public function remove_views($views) {

        if (!$this->apply_restrictions()) {
            return $views;
        }
        foreach($this->not_allowed_statuses as $status) {
            if (isset($views[$status])) {
                unset($views[$status]);
            }
        }

        return $views;
    }
    // end of remove_views()        


    private function remove_status_query($query) {

        foreach ($this->not_allowed_statuses as $status) {
            if (is_array($_GET['post_status'])) {
                foreach ($_GET['post_status'] as $key => $value) {
                    if ($value == $status) {
                        unset($_GET['post_status'][$key]);
                        unset($_REQUEST['post_status'][$key]);
                        unset($query->query_vars['post_status'][$key]);
                        unset($query->query['post_status'][$key]);
                    }
                }
            } elseif ($_GET['post_status'] == $status) {
                unset($_GET['post_status']);
                unset($_REQUEST['post_status']);
                unset($query->query_vars['post_status']);
                unset($query->query['post_status']);
            }
        }

    }
    // end of remove_status_query()


    public function filter_status($query) {

        if (!$this->apply_restrictions()) {
            return;
        }

        $screen = get_current_screen();
        if (empty($screen) || $screen->id !== 'edit-shop_order') {
            return;
        }

        if (!isset($_REQUEST['post_status'])) {
            $query->set('post_status', array('wc-processing')); // the 1st from allowed statuses according to the views order
        } else {
            $this->remove_status_query($query);
        }
    }
    // end of filter_status()


    public function block_status() {
        global $post;

        if (!$this->apply_restrictions()) {
            return;
        }

        if (empty($post)) {
            return;
        }
        if ($post->post_type!=='shop_order') {
            return;
        }

        if (in_array($post->post_status, $this->allowed_statuses)) {
            return;
        }

        // redirect user to the Orders list
        $url = get_site_url() .'/wp-admin/edit.php?post_type=shop_order';
        if (headers_sent()) {
            ?>
            <script>
                document.location.href = '<?php echo $url; ?>';
            </script>
            <?php
        } else {
            wp_redirect($url);
        }
        die;

    }
    // end of block_status()

}
// end of URE_WC_Order_Status_Restrictor class

但不能添加多个角色 我需要添加多个用户角色并且每个角色都有受限列表

例如

角色 1 仅展示 2 秩序雕像 角色 2 仅展示 1 雕像 角色 3 仅展示 3 雕像

当我尝试复制此功能时,它给了我错误

标签: wordpresswoocommerce

解决方案


推荐阅读