Last active
June 24, 2019 12:49
-
-
Save DevWael/967c8b5037e37d2e8bf5c1280a753349 to your computer and use it in GitHub Desktop.
Custom user roles for products and orders management in woocommerce (Products Manager & Orders Manager)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//put the following code in a custom plugin file | |
//no need for customizing the code, just go and create new users with the new user roles appeared in dashboard | |
register_activation_hook( __FILE__, 'wsd_plugin_activate' ); | |
function wsd_plugin_activate() { | |
add_role( 'wsd_orders_manager', esc_html__( 'Orders Manager', 'wsd' ), [ | |
'read' => true, // must have to even access /wp-admin | |
'edit_posts' => true, // without this the user is redirected to WooCommerce account page | |
'publish_posts' => false, | |
'read_shop_order' => true, | |
'edit_shop_orders' => true, // show orders in menu | |
'edit_others_shop_orders' => true, // must have | |
] ); | |
add_role( 'wsd_product_manager', esc_html__( 'Products Manager', 'wsd' ), [ | |
'delete_others_products' => true, | |
'delete_private_products' => true, | |
'delete_product' => true, | |
'delete_product_terms' => true, | |
'delete_products' => true, | |
'edit_others_products' => true, | |
'edit_private_products' => true, | |
'edit_product' => true, | |
'edit_product_terms' => true, | |
'edit_products' => true, | |
'edit_published_products' => true, | |
'manage_links' => true, | |
'manage_product_terms' => true, | |
'manage_woocommerce' => true, | |
'read' => true, | |
'read_private_pages' => true, | |
'read_private_posts' => true, | |
'read_private_products' => true, | |
'read_product' => true, | |
'read_shop_webhook' => true, | |
'unfiltered_html' => true | |
] ); | |
} | |
add_action( 'admin_menu', 'wsd_remove_woocommerce_menu_user', 999 ); | |
function wsd_remove_woocommerce_menu_user() { | |
global $current_user; | |
$user_roles = $current_user->roles; | |
$user_role = array_shift( $user_roles ); | |
if ( $user_role == "wsd_product_manager" ) { | |
remove_menu_page( 'woocommerce' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment