Show for the role WooCommerce orders with selected status only

Is it possible to show for the role WooCommerce orders with the selected status(es) only?
User Role Editor Pro does not include such functionality directly. It’s possible to add such kind of restriction to User Role Editor (URE) using URE built-in additional options for role interface and piece of the custom PHP code.

User with role restricted this way:

Additional role option

will see just “Processing” view at the “WooCommerce->Orders” page:

“Processing” view only

Setup code below as a “Must Use” plugin in order to add described additional option for the role inside User Role Editor:

<?php
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;
}
if ( !class_exists('URE_Lib_Pro') ) {
return;
}
$lib = URE_Lib_Pro::get_instance();
$page = $lib->extract_command_from_url( $_SERVER['REQUEST_URI'], false );
if ( $page!=='edit.php') {
return;
}
$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;
}
if ( !function_exists('get_current_screen') ) {
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

Just change commented posts views and add/remove post statuses for available shop orders in order to customize this code for your own needs.

Share