There are a lot of people who use WooCommerce to sell their products to customers and wholesalers. For those people who are it would be nice to be able to display all orders that are made by wholesalers or customers. In this post I will show you how you can easily add a new drop down to filter by user role, just next to the ‘Search for a customer’ drop down in the order overview in the admin.

Adding a peace of code

In order to create the drop down, and the functionality to filter the orders on user role, I’ve created a peace of code that can be added to your themes functions.php. Its best to add it there as it are only two functions, and not really worthy of creating a plugin if you just want to use this functionality. Of course feel free to create a separate plugin if you want to 🙂

Thats it!

That will do everything you will need to add a drop down to the shop order admin area of WooCommerce.

woocommerce-filter-orders-on-user-role

  • Jeroen Sormani

    I'm a professional WordPress plugin developer on a mission to create the best plugins for my clients. I'm specialised in developing general WordPress, WooCommerce and Easy Digital Downloads plugins.

    Interested in talking about a WordPress project? Get in touch!

    Follow me on Twitter
  • 24 thoughts on “Order WooCommerce orders per user role

    Grace July 15, 2015 at 3:53 pm

    Thank you – this helps a lot 🙂 Nicely done Jeroen!
    Any thoughts on how to dedicate a row color or icon of some sort to each role in the display for quickly spotting it before filtering?

    jeroen July 15, 2015 at 4:09 pm

    You’re welcome, happy to help 🙂

    I think a icon would be the hardest to do, these are assigned per order status, and I wouldn’t recommend adding new ones just for wholesalers.

    A row background color should be able to be accomplished with a bit of CSS and some code creativity, but I haven’t dived into such thing.

    Have you been successful in implementing the code? One thing I was kinda bugged by was the long list of unused user roles so I’m curious what your thoughts are on that.

    Cheers,
    Jeroen

    Dale November 23, 2015 at 8:45 pm

    Hi Grace, here is a snippet of code that you can add to your theme’s functions.php file to add custom row colors based on customer’s role: http://pastebin.com/uygF6NsJ

    Jordan October 27, 2015 at 12:20 pm

    Hi, i have a problem with this code.
    This il looking for “post_author” id but in my case, all the orders have the same post_author value (1). How can i fixe it to have the customer id in that fiel instead of 1 (maybe the admin value).

    Sorry for my bad english 🙂

    jeroen October 30, 2015 at 9:24 am

    Hi Jordan,

    This is a very specific code snippet. If you want to modify the purpose to something it is possible but may require a bit more work.
    If you want to get the orders of one specific user, you can do that already by default in WooCommerce (edit a order, and click the ‘View other orders →’ button). It uses a URL parameter to select all those users (so you won’t need all of the code in the code snippet).

    Cheers,
    Jeroen

    Dale Higgs November 23, 2015 at 7:46 pm

    Jordan, I had the same issue. All of my WooCommerce orders had the post_author as “1” instead of being the customer’s user ID. Here is a modified version that uses the “_customer_user” meta field instead of post_author: http://pastebin.com/cEJ864ci

    Jereon, this modified version also includes the added feature of hiding all of the unused user roles. 🙂

    jeroen November 24, 2015 at 3:39 pm

    Looks great from here Dale!

    Haven’t tested it yet, but how is the performance on the order count query? If you have a ton of orders I guess this can be just a cinch slow(ish). How about adding a LIMIT 1 (as you only want to know its not empty, right?) ?

    Cheers,
    Jeroen

    Deb January 22, 2016 at 9:56 am

    Hi Jeroen

    I wonder if you could help? I have tried adding this code and although it worked to add in the dropdown filter, the filter itself wouldn’t work to show the orders for the selected role. It either showed no results or seemed to jump to show the Products list
    Any help would be appreciated
    Thanks in advance
    Deb

    jeroen January 22, 2016 at 11:13 pm

    Hi Deb,

    It didn’t seem to work, and I just made some changes to the code posted on the page, which should fix things.

    Let me know how things work out for you 🙂

    Thanks,
    Jeroen

    Kenneth Vigue January 22, 2016 at 10:03 pm

    Good afternoon!
    Does this still work with the newest version of WordPress? I copied and pasted the code correctly but when I filtered by user role…it returns no values.

    jeroen January 22, 2016 at 10:29 pm

    Hi Kenneth,

    I’ve just tested this on WP 4.4.1 and WC 2.5 and it works over here.
    Are you sure you’ve selected a user role that has orders?

    Cheers,
    Jeroen

    jeroen January 22, 2016 at 11:14 pm

    Hi Kenneth,

    I spoke to soon,, it indeed didn’t work, but I just made some changes that should fix everything. Re-copy the code snippet and you should be good to go 😉

    Cheers,
    Jeroen

    Kenneth Vigue February 2, 2016 at 8:28 pm

    Hey Jeroen:

    I copied the new version…but it’s still not returning any values? The user roles are set correctly. Is it possible to contact you to take a look?

    – Ken

    jeroen February 3, 2016 at 9:51 pm

    Hi Ken,

    What WP / WC versions are you using?
    What are the user roles you’re using?
    Is it not showing any results at all, or are they not filtered correctly?

    Thanks,
    Jeroen

    Ari June 14, 2017 at 8:18 pm

    Hi Jeron, I would love to use your snippet but don’t know how to I create a user role that is connected to specific orders. Could you help me with that and then I can use your code to filter?

    Jeroen June 15, 2017 at 8:42 am

    Hi Ari,

    The filtering is based on the user role of the customers that have made an order. The user roles are thus based on the customer of said order.

    If you want to create a new user role I’d advise a plugin like User Role editor.

    Cheers,
    Jeroen

    Donna January 23, 2016 at 1:14 am

    Hi Jeroen,

    Is there anyway I can set-up the orders page in the back end of Woomerce to filter the different pick-up locations. I have plugged in the Local Pick-ups Plus as we have multiple pick-up locations (no shipping) and I would like the staff at the different stores to only see their orders not all orders?

    I am also looking for the best option to display a build you own sandwich option
    in our online store. I have installed product bundles and composite products but it is a bit clumsy or is it just my config?
    Do you have a plugin that works for this application. Ultimately I would like customers to just choose the options they want, the composite (stepped) config makes you choose every component even though it is ticked optional otherwise it wont let you proceed to the next component. Is this just my crappy set-up? and maybe I could get a developer to configure this properly?
    The sandwich options have a lot of variable products included such as three type of lettuce, 10 breads….

    Thanks
    Donna

    jeroen January 23, 2016 at 5:53 pm

    Hi Donna,

    It is possible to setup a filter based on the pick-up locations, but that does require quite some changes as per the code snippet on this page (but is would be a good base).

    Re sandwich builder:
    It would all depend on your exact needs. I think these plugins may fit your needs:
    https://www.woothemes.com/products/composite-products/
    https://docs.woothemes.com/document/product-add-ons/

    If you’d like to talk further about this, feel free to contact me: http://jeroensormani.com/hire-me 🙂

    Cheers,
    Jeroen

    Deb January 24, 2016 at 10:08 pm

    Hi Jeroen
    Just tried the rewritten version, but still not working unfortunately. Would it be better to contact you directly?

    Thanks
    Deb

    Adam January 26, 2016 at 6:37 pm

    Hi I have just tried this piece of code both in my funtions.php file and as a plugin and its causing a fatal error on line 35

    Parse error: syntax error, unexpected ‘<' in /var/www/vhosts/claracreate.com/httpdocs/wp-content/plugins/order-filter-by-role/orderfilterbyuserrole.php on line 35

    jeroen January 27, 2016 at 10:10 am

    Hi Adam,

    My best guess is that you copied too much of the code snippet into that script..
    If you copy it into an existing script you shouldn’t include the `< ?php` part. (I wrote a peace about code snippets some time ago: https://shopplugins.com/how-to-add-a-code-snippet-to-your-site/) in case you need more assistance with it.

    Hope that fixes things for you 🙂
    Jeroen

    PN March 15, 2016 at 10:42 pm

    This piece of code has been of immense help, thanks Jeroen!

    Mike Hemberger October 21, 2016 at 9:50 pm

    Super cool, thanks for this! Working well as of WP 4.6.1 and Genesis 2.4.2.

    Leave a Reply