Skip to content

Instantly share code, notes, and snippets.

@supercleanse
Created April 9, 2014 17:29
Show Gist options
  • Save supercleanse/10294791 to your computer and use it in GitHub Desktop.
Save supercleanse/10294791 to your computer and use it in GitHub Desktop.
Updating roles via a lookup array when MemberPress Transaction completes
<?php
/*
Plugin Name: MemberPress Products to User Roles
Plugin URI: http://memberpress.com
Description: Assigns a member to a User Role depending on what Product they purchased.
Version: 1.0.0
Author: Caseproof, LLC
Author URI: http://caseproof.com
Copyright: 2004-2013, Caseproof, LLC
*/
function memberpress_user_role($txn)
{
$user = get_user_by('id', $txn->user_id);
//user doesn't exist for some reason, or is an admin - we don't want to wipe out the admin's role haha
if($user === false || user_can($txn->user_id, 'manage_options'))
return;
$role_ids = json_decode('{
"bronze": [2,5,8],
"silver": [3,6],
"gold": [4,7],
"platinum": 9
}');
foreach( $role_ids as $role => $ids ) {
if( !is_array($ids) ) { $ids = array($ids); }
if( in_array($txn->product_id, $ids) ) {
return $user->set_role($role);
}
}
}
add_action('mepr-txn-status-complete', 'memberpress_user_role');
@sidekickDan
Copy link

Hi Blair, this works great. Is there a way to modify the code to remove a person from a role or reset them to another one, upon a transaction, subscription expiration? I found your other code related to doing something when mepr-event-transaction-expired, but i can't seem to get it to work the other direction :)

// Works as expected
function memberpress_user_role($txn)
{
  $user = get_user_by('id', $txn->user_id);
  //user doesn't exist for some reason, or is an admin - we don't want to wipe out the admin's role haha
  if($user === false || user_can($txn->user_id, 'manage_options'))
    return;
  $role_ids = json_decode('{
    "chamber-membership-levels":   [204,218,219,220,221]

  }');
  foreach( $role_ids as $role => $ids ) {
    if( !is_array($ids) ) { $ids = array($ids); }
    if( in_array($txn->product_id, $ids) ) {
      return $user->set_role('editor');
    } 
    else {
        return $user->set_role('author');
    }
  }
}
add_action('mepr-txn-status-complete', 'memberpress_user_role');

// Cant seem to get it to work to remove or change role on expiration
function catch_txn_expired($event)
{
  $txn = new MeprTransaction($event->evt_id); //evt_id should be the id of a transaction
  $user = new MeprUser($txn->user_id);

   $users_memberships = $user->active_product_subscriptions();
  //Make sure they're not still subscribed to this membership somehow
  if(!empty($users_memberships)) {
    if(!in_array($txn->product_id, $users_memberships, false)) {
      //They are actually expired so remove them from your third party app here
      return $user->set_role('author');
    }
    //else the user is still subscribed to this membership, so do nothing
  }
  else {
    //The user has no memberships, so it's definitely expired
    return $user->set_role('author');
  }
}
add_action('mepr-event-transaction-expired', 'catch_txn_expired');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment