Home / How To Allow Shop Managers To Edit Wholesale Users
Duplicate Snippet

Embed Snippet on Your Site

How To Allow Shop Managers To Edit Wholesale Users

Allow Shop Managers to edit and promote wholesale users by adding capabilities

Code Preview
php
<?php
/**
 * Allow Shop Managers to edit and promote wholesale users.
 */
function wws_add_shop_manager_user_editing_capability() {
    $shop_manager = get_role('shop_manager');
    if ( ! $shop_manager ) {
        return;
    }
    // Only add capabilities if they don't already exist
    if ( ! $shop_manager->has_cap('edit_users') ) {
        // Core WP + WooCommerce capabilities
        $shop_manager->add_cap('list_users');
        $shop_manager->add_cap('edit_users');
        $shop_manager->add_cap('create_users');
        $shop_manager->add_cap('promote_users');
        $shop_manager->add_cap('add_users');
    }
}
add_action('init', 'wws_add_shop_manager_user_editing_capability');
/**
 * Allow Shop Managers to edit wholesale roles.
 */
function wws_allow_shop_manager_wholesale_roles( $roles ) {
    $wholesale_roles = array(
        'wholesale_customer',
        'wholesale_gold',
    );
    foreach ( $wholesale_roles as $role ) {
        // Only add if the role exists in WordPress
        if ( get_role( $role ) && ! in_array( $role, $roles, true ) ) {
            $roles[] = $role;
        }
    }
    return $roles;
}
add_filter('woocommerce_shop_manager_editable_roles', 'wws_allow_shop_manager_wholesale_roles', 20);

Comments

Add a Comment