Changing second user role while updating/downgrading membership level - s2member

here is the thing that i want to achive:

s2_member_level0 has the second role "customer" - when upgrading the account via shortcode provided by s2member the account should upgrade to s2member_level1 and change it's second role to "vendor"

and

s2_member_level1 has the second role "vendor" - when downgrading the account via shortcode provided by s2member the account should downgrade itself to s2member_level0 and change it's second role back to "customer"

i'm setting the roles for the different accounts while they are activated with this function in my child theme:

add_action( 'bp_core_activated_user', 'add_secondary_role_new', 10, 1 );

function add_secondary_role_new( $user_id ) {
  global $members_template;
  $user = get_user_by('id', $user_id);
    if (user_can($members_template-member-id,'s2member_level1')){
      $user-add_role('vendor');
    }
    else{
      $user-add_role('customer');
    }
}

and i want to change the user roles with these function which basically update the userdata in the database:

function change_role_while_upgrade_account(){
  global $members_template;
  $user = get_user_by('id', $user_id);
  $user = wp_update_user(array('ID'=$user,'role'='vendor'));
}

function change_role_while_downgrade_account(){
  global $members_template;
  $user = get_user_by('id', $user_id);
  $user = wp_update_user(array('ID'=$user,'role'='vendor'));
}

So the question is: how can i use this functions (if they are working) with the shortcodes that s2 member generates? Does anyone have a clue and can helpme figure it out?

my shortcodes for upgrading:

[s2Member-PayPal-Button modify="1" level="1" ccaps="" desc="Upgrading Account to Premium" ps="paypal" lc="" cc="EUR" dg="0" ns="1" custom="www.netz-mitteldeutschland.de" ta="0" tp="0" tt="Y" ra="2799.00" rp="1" rt="Y" rr="1" rrt="" rra="1" image="default" output="button" /]

shortcode for unsubscriping:

[s2Member-PayPal-Button cancel="1" image="default" output="anchor" /]

Thanks for your responses

Topic s2member woocommerce-offtopic user-roles Wordpress

Category Web


I found a solution. Here it is:

//Update Roles automatically when Membership Level changes
function rb_update_user_role( $user_id) {
   $user = new WP_User( $user_id );
   $user_meta=get_userdata($user_id);
   $all_role = $user_meta->roles;

   foreach ($all_role as $role_value){
     if ($role_value === 'vendor'){
         $user->remove_role( 'vendor' );
     }
     if ($role_value === 'customer'){
         $user->remove_role( 'customer' );
     }
     if ($role_value === 's2member_level1'){
         $user->remove_role( 's2member_level1' );
         $role = 's2member_level1';
     }
     if ($role_value === 'subscriber'){
         $user->remove_role( 'subscriber' );
         $role = 'subscriber';
     }
   }
   if ( $role === 'subscriber' ) {
    unset($new_role);
    $new_role = array($role => 0 ,'customer' => 1) ;
    update_user_meta($user_id, 'wp_capabilities', $new_role);
   } 

   if ($role === 's2member_level1'){
    unset($new_role);
    $new_role = array($role => 0,'vendor' => 1) ;
    update_user_meta($user_id, 'wp_capabilities', $new_role);
   } 
}
add_action( 'profile_update', 'rb_update_user_role', 99, 1 );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.