I'm working on a portal (it's proof of concept) that allows Shop Managers to approve or reject an order when they review it. To approve the order, the Shop Manager adds a Purchase Order number and updates the order. This works in the back-end.
The difficulty I'm having is allowing the Shop Manager to see all orders via the front-end. The code I've used is as follows:
// Display all orders for shop managers on the front-end
add_filter('woocommerce_my_account_my_orders_query', 'show_all_orders_for_shop_manager', 10, 2);
function show_all_orders_for_shop_manager($args, $current_user) {
if (current_user_can('shop_manager')) {
$args['customer'] = '';
}
return $args;
}
Each time I try to use the orders screen, I get a narrowed version of the orders screen, with content overlapping, some CSS missing and the message "There has been a critical error on this website. Learn more about troubleshooting WordPress."
I know it can be done - I've seen something similar elsewhere (can't for the life of me remember where). I've also been able to test things out using just the customer role, but that restricts you to just your orders, not every order.
You get an error because the filter hook woocommerce_my_account_my_orders_query
has only $args
as available argument, but not $current_user
.
Note that to target a specific user role, you should not use WordPress current_user_can()
which is made to target user capabilities, instead you should better use WooCommerce wc_current_user_has_role()
conditional function.
To finish, you should better use PHP unset()
function to remove 'customer' argument instead.
So try the following replacement code:
add_filter( 'woocommerce_my_account_my_orders_query', 'show_all_orders_to_shop_manager_role', 20 );
function show_all_orders_to_shop_manager_role( $args ) {
if ( wc_current_user_has_role('shop_manager') && isset($args['customer']) ) {
unset($args['customer']);
}
return $args;
}
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
Note that the shop manager will not be able to view orders belonging to other customers, because WooCommerce doesn't allow it.