Hiding some users and customers to shop managers based on the product they bought

132 Views Asked by At

here’s my question. I hope you can help since I am a bit stuck (not beeing good at PHP…).

I build a sport school website, allowing customers to purchase memberships. For this I use WooCommerce, Memberships and Subscription.

Sports for which classes are given are judo and football, and the two teachers are shop managers for their own sport courses only. For obvious reasons, none of them needs to see each other students (as customers as well as users), each other students’ orders, or even each other products.

I already dealt with products and orders.

Each manager sees the products he created. Such ownership can be granted by the administrator after he created the products if need be. Here’s my code, borrowed from the web.

/*Add authorship to products*/

add_action('init', 'function_to_add_author_woocommerce', 999 );
function function_to_add_author_woocommerce() {
    add_post_type_support( 'product', 'author' );
}

/*Keep only the current logged in user products, except for admins*/

add_action( 'pre_get_posts', 'admin_pre_get_posts_product_query' );
function admin_pre_get_posts_product_query( $query ) {
    global $pagenow;

    if( current_user_can( 'administrator' ) ) {
        return;
    } else if ( is_admin() && 'edit.php' == $pagenow && isset($_GET['post_type']) && 'product' === $_GET['post_type'] ) {
        $query->set( 'author', get_current_user_id() );
    }
}

As for the orders, each manager sees orders linked to a product categorized as « judo » or « football ». Each of these categories are then subdivided within subcategories (age related) but that’s irrelevant to the sorting problem.

I am using Filter WooCommerce orders by shop manager based on assigned taxonomy terms answer code and here are my own settings for it:

/*Set the parameters*/

function orders_by_shop_manager_settings(){
    return array(
        // Can be 'name', 'slug' or 'term_id'
        'field'     => 'slug',
        // Taxonomy we want to focus on
        'taxonomy'  => 'product_cat',
        // ID of the shop manager / category we want to show
        'assignments' => array(
            '2'     => array('judo'),
            '3'    => array('football'),
        )
    );
}

So what remains is to take care of users and customers sorting – and I’m really lost there.

Here are the basic settings I have : each user/customer can purchase one product only and have access to a single membership plan (one account per product/plan). So I don’t need to build a complex « several products per customer » or « several memberships per customer » rule. What I would like is that the judo shop manager sees only customers and users owning a membership to which they gained access through a judo-related product, and the football shop manager only customers and users owning a membership to which they gained access through a football-related product.

I also would like each manager not to list other managers (avoiding any on-top security issues if one manager account is compromised at some point).

The shop manager list is short and should not change a lot so I’m confortable with a solution such as for sorting orders above. Manually adding each manager’s ID’s and their own settings is just fine to me.

Any idea? It would be of great great help.

0

There are 0 best solutions below