Created
July 7, 2024 15:14
-
-
Save shameemreza/52d0aa883e8cf1b81269f9ed3a071ba8 to your computer and use it in GitHub Desktop.
Change the user role on purchase in WooCommerce
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
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