Skip to content
Advertisement

Allow access to any order on front-end on WooCommerce

My goal is to allow certain role to view any order in shop. While displaying all orders in “My Account” page is relatively easy, accessing particular order created by different user always ends up with “Invalid order. My account” error.

So far I have added custom query on woocommerce_my_account_my_orders_query filter, so that one can view all orders made by accounts with the same role. I set role permissions to full (as per this link, same permissions as for Shop Manager and Administrator, however those roles can’t view all orders by default either: https://github.com/woocommerce/woocommerce/blob/ee01d4219282387c2975ef4594677453c1dd7a0e/includes/class-wc-install.php#L1052

I was thinking of creating custom view-order.php template, however I would rather use wc_get_order() to get entire order object at once. Has anyone came across such challenge?


Here’s how I added new role:

JavaScript

Here’s how I modified orders query so far:

JavaScript

Advertisement

Answer

Okay so here’s how I solved it. I ended up creating a copy of view-order.php template that I would serve to users that belong to particular role. In my child theme directory I created woocommerce/myaccount/view-my-order.php file, which contains the following before any content:

JavaScript

The above is to make sure that logged user can view orders only when customer had the same role.

Then in functions.php I put the following:

JavaScript

It all can be further optimized, but you get the idea.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement