UserWoocommerce

Add a Wholesale user role in Woocommerce without a plugin (cloning method)

The Default WordPress and WooCommerce User Roles:

WordPress uses user roles to give specific read and edit permissions to each user. This, makes it very easy and secure to manage your websites users and what each one can and can’t do.

Here are the most common WordPress user roles:

  • Superadmin: (only in multisites) the Superadmin, has all the admin rights, plus he manages the creation and deletion of sub-sites.
  • Administrator: The administrator has complete control over the website and can do anything.
  • Editor: Editors can create, edit, and publish posts, pages, and they can also manage comments.
  • Author: Authors can create and publish posts but can edit or delete only his own posts (also he cannot upload images).
  • Contributor: Contributors can create posts but can’t publish them. Also he can’t edit or delete other users’ posts.
  • Subscriber: Subscribers can only read posts and leave comments.

If you have Woocommerce installed and activated, you’ll also get two additional user roles:

  • Shop Manager: Shop managers can manage all WooCommerce settings, create and edit products, view reports, plus use the WordPress editor.
  • Customer: Customers can view and edit their account info, create orders and view their own orders.

Keep in mind, you are able to create or edit these roles by adding or removing rights.

In this example we clone an existing user role (in this case, the ‘customer’ role) to create a new role called ‘wholesale_customer’. It’s a good starting point for creating a new role based on an existing one with similar capabilities.

So in more details, this code snippet clones the ‘customer’ role and creates a new role called ‘wholesale_customer’. Once you’ve run this code, you can use the ‘wholesale_customer’ role for your wholesale users.

    
add_action('init', 'wholesale_customer_role_clone'); function wholesale_customer_role_clone() { global $wp_roles; if (!isset($wp_roles)) $wp_roles = new WP_Roles(); // Assuming 'customer' role exists in your WooCommerce setup $customer_role = $wp_roles->get_role('customer'); // Clone 'customer' role to create 'wholesale_customer' role $wp_roles->add_role('wholesale_customer', 'Wholesale Customer', $customer_role->capabilities); }

Leave a Reply