My shop sells clothing, and while a majority of customers in user groups make their purchases through our webshop, some live close by and prefer to come in in person to try sizes and whatnot. Specific user groups have restrictions on what their users are allowed to buy, and currently, the only way I have found to easily see what they are permitted to buy is to make a user for their group that I can then log on to.
Assuming a solution does not already exist, I would like to make a page on our website (this can only be seen and accessed by admins/superadmins) that has a dropdown which contains every user group by name (preferably alphabetically), and when chosen, it will load in every product that user group has access to, presumably utilizing a querystring based on the user group ID.
Is this possible to do, and if so, where do I start? I would need to be able to get all groups programmatically for the dropdown list, and then I would need to be able to take the ID from a given group and feed it to a product list/archive in a way that makes it show only that group's products, instead of going off of whatever permissions the current logged in user has.
For this use case, I think the simplest approach would be to use a plugin like https://wordpress.org/plugins/login-as-user/ - when a customer comes in person, you can just log in as that customer and browse the shop to see exactly what products and prices are available to them.
If the customer doesn't already have an account with you, you could also create a sample user for each group/role and use those sample accounts when needed.
What you described with building a custom admin page is certainly possible as well, but it's more complex to achieve. If you'd prefer to go that route, I can share more info on how to get groups and filter products programmatically - just let me know.
The solution you mention with a sample account is what we've been doing, with each group having a dummy user with no permissions beyond being able to view products. The general issue with this solution has been how it requires whoever is manning the store to either open an incognito browser or even a secondary browser to then log in on the user belonging to the customer's group, which means they also have to look up the info for the dummy user in a shared sheet, all of which eats more time than feels necessary. While they could log out of the admin user to do this too, the PCs get used by multiple people at a time whenever the store is busy, and they need to be able to view orders and such on the admin parts of the site.
I would like to try my hand at making the custom admin page as I described it, any help is most welcome, please and thank you.
This is a bit tricky, actually fetching the filtered product list programmatically isn't really feasible with custom code. The visibility logic in B2BKing is quite complex (thousands of lines including custom caching), so I do not see a good way to achieve it that way.
However, I think there's a simpler approach that could work for you: instead of trying to display products on a custom page, you could build a quick "switch group" button that temporarily changes your admin user's group. That way you can just browse the shop normally and see exactly what that group sees.
To set a user's group, you just need to update two user meta keys:
- b2bking_b2buser - set to "yes" for B2B users - b2bking_customergroup - set to the group ID (the post ID from the dropdown)
You could even add a timeout or "revert" mechanism so it automatically switches back to your normal admin state after a few minutes.
This would be much simpler to build and would give you the full accurate product view without needing to log out or open incognito windows.
Something important to note here: each time you change a user's group (by setting those 2 meta keys), or reverting the group back to the original, you must run this cache clear code:
My shop sells clothing, and while a majority of customers in user groups make their purchases through our webshop, some live close by and prefer to come in in person to try sizes and whatnot. Specific user groups have restrictions on what their users are allowed to buy, and currently, the only way I have found to easily see what they are permitted to buy is to make a user for their group that I can then log on to.
Assuming a solution does not already exist, I would like to make a page on our website (this can only be seen and accessed by admins/superadmins) that has a dropdown which contains every user group by name (preferably alphabetically), and when chosen, it will load in every product that user group has access to, presumably utilizing a querystring based on the user group ID.
Is this possible to do, and if so, where do I start? I would need to be able to get all groups programmatically for the dropdown list, and then I would need to be able to take the ID from a given group and feed it to a product list/archive in a way that makes it show only that group's products, instead of going off of whatever permissions the current logged in user has.
Hi Benjamin,
Thanks for reaching out,
For this use case, I think the simplest approach would be to use a plugin like https://wordpress.org/plugins/login-as-user/ - when a customer comes in person, you can just log in as that customer and browse the shop to see exactly what products and prices are available to them.
If the customer doesn't already have an account with you, you could also create a sample user for each group/role and use those sample accounts when needed.
What you described with building a custom admin page is certainly possible as well, but it's more complex to achieve. If you'd prefer to go that route, I can share more info on how to get groups and filter products programmatically - just let me know.
Kind regards,
Stefan
Hi Stefan,
The solution you mention with a sample account is what we've been doing, with each group having a dummy user with no permissions beyond being able to view products. The general issue with this solution has been how it requires whoever is manning the store to either open an incognito browser or even a secondary browser to then log in on the user belonging to the customer's group, which means they also have to look up the info for the dummy user in a shared sheet, all of which eats more time than feels necessary. While they could log out of the admin user to do this too, the PCs get used by multiple people at a time whenever the store is busy, and they need to be able to view orders and such on the admin parts of the site.
I would like to try my hand at making the custom admin page as I described it, any help is most welcome, please and thank you.
Hi Benjamin,
Thanks for explaining this in detail. I see what you mean.
I'd be happy to share more info on how you might approach a custom page to do this. Here's what I'd suggest:
1. Getting Groups for the Dropdown
You can use something like this to display all groups alphabetically:
groups = get_posts(array( 'post_type' => 'b2bking_group', 'post_status' => 'publish', 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' )); foreach ($groups as $group) { echo '<option value="' . esc_attr($group->ID) . '">' . esc_html($group->post_title) . '</option>'; }2. Regarding Products
This is a bit tricky, actually fetching the filtered product list programmatically isn't really feasible with custom code. The visibility logic in B2BKing is quite complex (thousands of lines including custom caching), so I do not see a good way to achieve it that way.
However, I think there's a simpler approach that could work for you: instead of trying to display products on a custom page, you could build a quick "switch group" button that temporarily changes your admin user's group. That way you can just browse the shop normally and see exactly what that group sees.
To set a user's group, you just need to update two user meta keys:
- b2bking_b2buser - set to "yes" for B2B users
- b2bking_customergroup - set to the group ID (the post ID from the dropdown)
You could even add a timeout or "revert" mechanism so it automatically switches back to your normal admin state after a few minutes.
This would be much simpler to build and would give you the full accurate product view without needing to log out or open incognito windows.
Something important to note here: each time you change a user's group (by setting those 2 meta keys), or reverting the group back to the original, you must run this cache clear code:
Hope this helps, let me know if you have any questions.
Kind regards,
Stefan