We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46708
    • 8 Posts
    Hi, i want to change usergroup for user who has paid for subscription. I wrote that code
    $user = $modx->getObject('modUser', array('username' => $username));
    if( $user ){
        $user->save();
        $user->joinGroup('5');}
    

    but it`s still not changing his usergroup, what can be wrong ?
    Thanks for answer ! [ed. note: mysontg last edited this post 10 years, 2 months ago.]
    • I don't believe that you need to save the user.

      I also found that using the group name rather than the ID worked for me.
      $user->leaveGroup('groupname');
      $user->joinGroup('groupname');
      


      Ah, figured out why group name worked and group ID didn't - the group ID can't be a string. Drop the single-quotes; that makes MODX look for a group named "5".
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 46708
        • 8 Posts
        I tried with group name and with id without single-quotes but still not working.
        Now i have:
        $user = $modx->getObject('modUser', array('username' => $username));
        if( $user ){
        
            // Assign new user to User Group / Role
           $user->leaveGroup(3);
        $user->joinGroup(5);}
        • Check the Reports->Error Log.

          Here is the exact snippet I used to test with
          <?php
          $username = 'test';
          $user = $modx->getObject('modUser', array('username'=>$username));
          $user->joinGroup(3);
          $user->leaveGroup(2);
          
          $usergroup = $user->getUserGroupNames();
          echo "<pre>";
          print_r($usergroup);
          echo "</pre>";
          
          return;


          If you are looking at the user in the Security menu, I found that I had to re-load the Manager page before my group changes showed up.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 46708
            • 8 Posts
            Thanks for help, now everything working OK smiley