2

I'm creating an internal-use "Dealer Club" using WooCommerce. It will be accessible by our external dealers as well as internal salespeople and admin types.

How do I show select products based on the different types of users (dealers, salespeople, other internal, etc.) and different geographic areas that the individual users service (East, West, all areas)?

The idea that I have so far....

  • Create the types of users and geographic areas using Cimy Extra User Fields plugin.
  • Tag each product appropriately (type and area).
  • Create a page for each product category that somehow queries only products with the correct tags to the users that match the "extra user fields". So only Eastern Salespeople would see items with tags "east" and "salespeople", for example.
  • The problems I see are how to queries the products based on tags, how to use the Cimy Extra User Fields to determine the classification of the user, and how to deal with hierarchy issues (users marked as Salespeople should be able to see all Dealer products as well).

However, if there is a "canned" solution that handles displaying certain products to certain groups, I'm all ears.

Thanks for suggestions!

3
  • 1
    Important sidenote: As your question mentions plugins only and show no previous effort and no code, I have to ask you to read the faq as both question types ("do this job for me" and "plugin recommendations") are off topic, so better prepare that your question might get closed.
    – kaiser
    Commented Apr 22, 2013 at 17:16
  • Thanks for your answer, kaiser. I didn't list all of the ideas I've tried so far as it would have been a MUCH longer question. I don't quite understand why "plugin recommendations" aren't allowed, but I appreciate your bringing this to my attention.
    – WebDevKev
    Commented Apr 22, 2013 at 19:00
  • 1
    They're actually "shopping questions" and banned network wide - not only on our site. And I was the one actually asking to make them off topic as they fast get outdated and don't add real value in a long term sight.
    – kaiser
    Commented Apr 22, 2013 at 19:52

1 Answer 1

3

Notes about users & roles

WordPress doesn't have a hierarchical role management system. I don't even know if there is or ever was a system that had this kind of setup. In general, "role" is just a name for a container that sums up specific capabilities. And those capabilities restrict access to something or grant it.

how to deal with hierarchy issues (users marked as Salespeople should be able to see all Dealer products as well)

So above quote simply is a misunderstanding of the concept of roles.

Who has a role, where does it reside, etc.?

A roles is assigned to a user. This is saved as user meta data in the DB and can be changed anytime. Either via code, or via the graphical UI in the admin back end. More can be read in the Codex, which I highly recommend.

Taxons/Terms (of a taxonomy) and Roles

Taxonomies are either flat (like post tags) or hierarchical (like categories) organized Meta Data that can be attached to object types like posts, pages, nav menu items or a custom post type. They can be attached to more than a single type of object to allow you to output archives of multiple object(post) types that share a taxonomy/have a connection.

As we already discussed roles: They aren't terms/taxons/a taxonomy. Now you'll have to ask yourself how you'd connect them. Possible ideas:

  • The "Slug"/URi/URL part -> bad idea as it might change and break
  • The label -> bad idea as it can be translated and changed
  • The unique ID -> bad idea as porting the DB from development to stage or production might be a pain and things wouldn't be descriptive: "What was ID 452 again?"

You can see that there's no real reliable or easy way to connect them unless you start building a nice UI to handle the task of taxon ID mapping.

Solution/Concept

What you could do is adding an additional user meta field that allows you to enter a specific template_route that will then be added as user meta data. Below you'll find a very simple plugin that adds an additional user meta entry field that allows you to enter a template_route name (should be lowercase and use _ as separators).

<?php
defined( 'ABSPATH' ) OR exit;
/** 
 * Plugin Name: (#96754) User Meta: Template route
 * Description: Adds a meta entry input field to user data. Only visible/accessible for administrator roles or other roles that have the <code>manage_options</code> capability assigned. Meta entry is used to show/hide parts in theme templates.
 */
# Version: 2013-04-22.1939

function wpse_96754_template_route( $methods, $user )
{
        if ( ! current_user_can( 'manage_options' ) )
            return $methods;

    $methods['template_route'] = 'Template Route';

    return $methods;
}
add_filter( 'user_contactmethods', 'wpse_96754_template_route', 10, 2 );

You can then check for this meta entry in a template like this:

if ( 'whatever' === get_user_meta( get_current_user_id, 'template_route', true ) )
{
    // show specific stuff
}

Of course there're dozens of other ways to do this, but this is the fastest way to approach your problem - just be aware of typos until you don't modify the output of the field and switch to select form field that allows you to select directly from a restricted array. This array should then as well should be accessible in your theme through a public custom API function, so you can avoid the chance of typos there.

2
  • I wasn't trying to use WP's role capabilities; I guess I was trying to create my own. I wanted to use something (Cimy Extra User Fields plugin) to record additional data about each user (type and area) and then somehow filter the products using that information. I appreciate the detailed and thoughtful answer, but I'm not sure that I understand it completely. I'll have to delve into "template parts" more thoroughly to get a better understanding before trying to implement your suggestion. Thanks again!
    – WebDevKev
    Commented Apr 22, 2013 at 19:02
  • Well, template parts are just atomic parts of a template splitted out of templates when you use them more than once. For example: If you display the same headline in archives and the index site. Anyway, you can leave it out and replace it with templates in my answer. What I showed you above is basically just what the plugin you mentioned does at well: Adding additional data input fields to user profiles that you can then check in a template.
    – kaiser
    Commented Apr 22, 2013 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.