Display specific WooCommerce products to different types of users

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)?

Read More

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!

Related posts

Leave a Reply

1 comment

  1. 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.