• Resolved digihelpbe

    (@digihelpbe)


    I have a woocommerce website that is used by different youth groups. each youth group has own events that they will sell. so I created for each youthgroup a parent product category ( eg. chiro,ksa, scouts, … ). and under this parent product categories, they can create their own sub product categories for each event.

    the problem is when they login to the back-end with the role shopmanager they see/manage all the products and all the orders of everybody.

    Can I create a new shopmanager role that only has access/manage all the products and orders of one specific product categorie (eg. ksa only) ? and how?

    thanks in advance,

    Jonay

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • @digihelpbe could this help?

    Modify Product Query:

    You’ll need to modify the product query in the WooCommerce admin to filter products based on the user’s role and the specific product category they are allowed to manage. You can do this by hooking into the pre_get_posts action.

    function custom_admin_product_query($query) {
        global $pagenow;
    
        if (is_admin() && $pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'product') {
            $user = wp_get_current_user();
            
            if (in_array('ksa_manager', $user->roles)) {
                // Modify the query to filter products based on the KSA category
                $query->set('tax_query', array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'slug',
                        'terms' => 'ksa', // Change this to the KSA category slug
                    ),
                ));
            }
        }
    }
    add_action('pre_get_posts', 'custom_admin_product_query');
    

    Repeat for Other Youth Groups:

    Repeat steps 1-4 for each youth group, customizing the role names, capabilities, and category filters accordingly.

    Remember to thoroughly test this setup on a staging site before implementing it on your live site, and always make sure you have a backup. Additionally, depending on your specific requirements, you might need to adjust and extend the code provided.

    You can find a similar problem and a short solution here – wordpress – Woocommerce: I restrict users from managing certain categories and products? – Stack Overflow and based on that better code example

    /**
     * Custom function to restrict users from managing certain categories and products
     */
    function custom_restrict_user_capabilities() {
        // Check if the user is an administrator (admin can manage all)
        if (current_user_can('administrator')) {
            return;
        }
    
        // Add product category slugs that should be restricted for non-admin users
        $restricted_categories = array('restricted-category-1', 'restricted-category-2');
    
        // Get the current user
        $user = wp_get_current_user();
    
        // Check if the user has capabilities to edit products
        if (in_array('shop_manager', $user->roles)) {
            // Modify the user capabilities to exclude managing restricted categories
            foreach ($restricted_categories as $category) {
                $user->remove_cap('edit_products');
                $user->remove_cap('edit_products');
                $user->remove_cap('edit_published_products');
                $user->remove_cap('publish_products');
                $user->remove_cap('delete_published_products');
                $user->remove_cap('delete_products');
                $user->remove_cap('delete_private_products');
                $user->remove_cap('edit_private_products');
            }
        }
    }
    add_action('admin_init', 'custom_restrict_user_capabilities');
    
    /**
     * Custom function to modify product queries based on user capabilities
     */
    function custom_modify_product_query($query) {
        // Check if the user is an administrator (admin can manage all)
        if (current_user_can('administrator')) {
            return $query;
        }
    
        // Get the current user
        $user = wp_get_current_user();
    
        // Check if the user has capabilities to edit products
        if (in_array('shop_manager', $user->roles)) {
            // Modify the product query to exclude products from restricted categories
            $restricted_categories = array('restricted-category-1', 'restricted-category-2');
            $tax_query = array(
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'slug',
                    'terms'    => $restricted_categories,
                    'operator' => 'NOT IN',
                ),
            );
            $query->set('tax_query', $tax_query);
        }
    
        return $query;
    }
    add_action('pre_get_posts', custom_modify_product_query');
    
    • This reply was modified 10 months, 2 weeks ago by Rok Megli?.

    Hi there @digihelpbe ??

    Thanks for reaching out to Woo Support!

    Can I create a new shopmanager role that only has access/manage all the products and orders of one specific product categorie (eg. ksa only) ? and how?

    From what I gather, WooCommerce Multisite (Google search with the term linked here) would be a good candidate for the needs of this set-up.

    Otherwise, feel free to consider if a plugin like User roles editor would cover the needs to a sufficient degree.

    I trust that points you in the right direction, but if you have more questions, let us know.

    We’re happy to help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘different shopmanager role for each product category’ is closed to new replies.