Tuesday, May 27, 2014

Change User role after payment successful on WooCommerce

Add below action and function on function.php file of the current theme.

add_action('woocommerce_thankyou', 'change_user_role_on_order_success');

function change_user_role_on_order_success($order_id ) {
$order = new WC_Order( $order_id );
$user_id = $order->user_id;
$wp_user_object = new WP_User($user_id);
if($wp_user_object->roles[0] != "administrator"){ // Do not change admin role
wp_update_user( array( 'ID' => $wp_user_object->ID, 'role' => "student" ) );
}
}

Change current user role to administrator.

Add below code to function.php file to change all user role to administrator.

//Change User role to any specific.
 $current_user = wp_get_current_user();
 $wp_user_object = new WP_User($current_user->ID);
 $wp_user_object->set_role('administrator');


Cheers.........