How to add permanent usergroup to user account upgraded?

CtrlV

Active member
Here is my Goal i can't achieved
  • Users buy Premium or VIP membership via Account Upgrade.
  • They are added to:
    • Patron usergroup as permanent and in
    • Premium usergroup or VIP usergroup (temporary, expires with upgrade).
  • When the upgrade expires:
    • They stay in Patron usergroup but
    • They are removed from Premium usergroup or VIP usergroup.
How to do this? the promotion only has "Apply this promotion while..." any help?
 
Not that it's of any use to you, but I was trying to solve a similar problem the other week and didn't figure out a way. The only thing that I could see that you could have set and not "unset" based on group membership were trophies, but you can't base a group upgrade on a particular trophy (just points), so that approach didn't work. In the end I had to accept I was going to have to do it with some external scripting. Of course there may be an existing add-on I just didn't find!
 
So just following up If you do take the approach of doing it totally outside of Xenforo with say a script running every so often via cron.

You'll want an SQL query to get the users who you want to place into your patron usergroup. So firstly identify the DB ID of the upgrade. You can find that just by looking at the URLs in the user upgrades part of the control panel - /admin.php?user-upgrades/upgrade-name-here.2/edit for instance has an ID of 2. So lets assume your upgrade has an ID of 2 and the patron group has an ID of 30 for the sake of an example:

SQL:
select xf_user_upgrade_active.user_id, xf_user.username, xf_user.secondary_group_ids 
from xf_user_upgrade_active left join xf_user on xf_user_upgrade_active.user_id = xf_user.user_id 
where xf_user_upgrade_active.user_upgrade_id in (2) and !find_in_set(30, xf_user.secondary_group_ids);

So we're looking at the active upgrades table - if they have bought your upgrade (ID 2) we'll find them in this table. Now I think you've probably got two upgrade IDs - Premium and VIP so I've done the query so you could do both (rather than a straight a = b) - since we have a where condition which is "where value is in this list" - where xf_user_upgrade_active.user_upgrade_id in (2) that could be 2,8 for instance if 2 and 8 were the upgrade IDs.

However we're probably not interested in users who are already in the Patron group (30 in this example) so we've got a extra condition to exclude anyone who's secondary group field contains that entry - and !find_in_set(30, xf_user.secondary_group_ids).

We do have to join in the xf_user table since we need access to those secondary groups, especially if we might then update them!

Anyhow that would give you the: user_id, username and secondary_group_ids for each user who has an active Premium or VIP upgrade, but is not in the Patron group.

Armed with this information you could then use the XenForo API (also read this) to update the secondary user groups of those users to include the Patron group by making a POST query to users/<user_id>/ for each user (<user_id> being the user's ID) and passing in secondary_group_ids[]=<full_list_of_groups_for_user> as the parameter (<full_list_of_groups_for_user> being their existing list of groups plus the extra patron one). Where possible I think it best to use the API to update things like this as it ensure any special XF logic is applied rather than just blindly tweaking the database (note the secondary group field in the DB is a binary field not just a plain string of comma separated values).

Anyhow that's how I'd do it. Now of course if you are happy with PHP and the XF framework you could do all that in an add-on or something probably using the various XF classes and be much neater about the whole affair. Alas that's beyond my pay grade.

If I were hacking this as a shell script to run periodically to add "upgraded" users into the Patron group I would be doing (untested) something like:
Bash:
#!/bin/sh

# MySQL command line to get our users, but we don't want all the fancy table markup or column names, just user IDs and groups please
# we'll pipe that directly into a while loop to read each line of output...
mysql xenforo_database_name -e 'select xf_user_upgrade_active.user_id, xf_user.secondary_group_ids from xf_user_upgrade_active
left join xf_user on xf_user_upgrade_active.user_id = xf_user.user_id
where xf_user_upgrade_active.user_upgrade_id in (2) and !find_in_set(30, xf_user.secondary_group_ids);' --batch --skip-column-names |
while read line
do
  # Extract the user ID and existing groups out from the line
  user_id=$(echo $line | cut -f1 -d ' ')
  user_groups=$(echo $line | cut -f2 -d ' ')

  # Create a new string with the extra group added on,
   # in this example the extra group is group 99
  new_user_groups="$user_groups,99"

  # Echo things back to the screen so we can see what we've set
  echo "Line: $line"
  echo "User: $user_id"
  echo "Existing Groups: $user_groups"
  echo "Desired Groups:  $new_user_groups"

  # Iterate over the list of groups we want and construct the curl options we'd need to set these groups
  options=$(echo $new_user_groups | awk -F "," '{for(i=1; i<=NF; i++) { printf " --data-urlencode %s", $i; }}')

  # Echo our curl payload and curl command to the screen
  echo "API payload: $options"
  echo "Curl Command: curl --header 'XF-Api-Key: secret-api-key' --header 'XF-Api-User: 1' --request POST 'https://forum.example.com/api/users/$user_id/' $options"
  echo

  # TODO actually execute the curl command to update the user's groups!
done

Obviously (I hope) putting in correct values for the XF database, API key, URL, user_upgrade_id, existing group and new group ID (99 in my script example above).

Note in that example I've not actually executed the curl command because I don't want anyone blindly copying and pasting without understanding what they are doing, partially also why it's quite verbose. I'd personally be using a more fully featured programming language, be that PHP, or whatever still I thought it might be interesting as a thought exercise to do it as a nice vanilla bourne shell script (other shells will no doubt have different escaping quirks), maybe it'll serve as a starting point, but you'd be better off and more robust reading the database and so forth using a proper library rather than hacking away at the output if you do go via this route.
 
I appreciate your help but its too complicated for me to do hard code. I want to make it simple in settings.
 
Fair enough. The only other way I can think of to do it "in settings" and it's not ideal is to use the badges add-on:

You could then award a (non recallable) badge to your subscribers based on their membership of the Premium or VIP groups.
Then do a Group Promotion into the Patron group for anyone with that particular badge into your Patron group

That would work since you can optionally have badges awarded, but then not recalled if the awarding condition changes. It wasn't an option in my own use-case as I didn't want the visibility of a badge, so I'd rather forgotten about it. However it's a nice add-on and would probably actually work okay with your use-case here - since having a nice "patron badge" (in addition to being in a patron group) isn't going to upset anyone!
 
Could you make a second free user upgrade that a member could opt into ?

Make a user upgrade for them to buy for a fee that promotes the user to the VIP group and set the desired expiry date
Make another user upgrade that is free and only available to members of the VIP group that does not expire that puts them in the patron group

Only downside of this is that it requires a second action from the user who would need to 'buy' a free upgrade
 
A mate of mine did this on his forum.
I'll tag him but there's no guarrantee that he will respond.
@BIG LLC you should tell them how you set up your premium membership program on bigfooty.
 
Fair enough. The only other way I can think of to do it "in settings" and it's not ideal is to use the badges add-on:

You could then award a (non recallable) badge to your subscribers based on their membership of the Premium or VIP groups.
Then do a Group Promotion into the Patron group for anyone with that particular badge into your Patron group

That would work since you can optionally have badges awarded, but then not recalled if the awarding condition changes. It wasn't an option in my own use-case as I didn't want the visibility of a badge, so I'd rather forgotten about it. However it's a nice add-on and would probably actually work okay with your use-case here - since having a nice "patron badge" (in addition to being in a patron group) isn't going to upset anyone!
Thanks this is another way i can consider.

Could you make a second free user upgrade that a member could opt into ?

Make a user upgrade for them to buy for a fee that promotes the user to the VIP group and set the desired expiry date
Make another user upgrade that is free and only available to members of the VIP group that does not expire that puts them in the patron group

Only downside of this is that it requires a second action from the user who would need to 'buy' a free upgrade
Thanks other than that downside is im simplifying also the usergroup and i think this can also achieve what i want.
 
Back
Top Bottom