Display specific WooCommerce products to different types of users

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.