Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shameemreza/52d0aa883e8cf1b81269f9ed3a071ba8 to your computer and use it in GitHub Desktop.
Save shameemreza/52d0aa883e8cf1b81269f9ed3a071ba8 to your computer and use it in GitHub Desktop.
Change the user role on purchase in WooCommerce
add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase', 10, 2 );
add_action( 'woocommerce_order_status_completed', 'change_role_on_purchase', 10, 2 );
function change_role_on_purchase( $order_id, $order ) {
$user = $order->get_user(); // Get the WP_User Object
// Check for "customer" user roles only
if( is_a( $user, 'WP_User' ) && in_array( 'customer', (array) $user->roles ) ) {
// Remove WooCommerce "customer" role
$user->remove_role( 'customer' );
// Add new role
$user->add_role( 'editor' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment