Skip to content

Instantly share code, notes, and snippets.

@cartpauj
Last active August 1, 2024 16:38
Show Gist options
  • Save cartpauj/9a8e0edde649d6fb3197508bc7b14313 to your computer and use it in GitHub Desktop.
Save cartpauj/9a8e0edde649d6fb3197508bc7b14313 to your computer and use it in GitHub Desktop.
Get all sub-accounts for a parent user in MemberPress Corporate Accounts
<?php
$user = MeprUtils::get_currentuserinfo();
$sub_user_ids = array();
if($user !== false) {
$transactions = $user->active_product_subscriptions('transactions');
if(!empty($transactions)) {
foreach($transactions as $txn) {
if(($sub = $txn->subscription()) !== false) {
//Recurring subscription
$ca = MPCA_Corporate_Account::find_corporate_account_by_obj_id($sub->id, 'subscriptions');
}
else {
//Non Recurring subscription
$ca = MPCA_Corporate_Account::find_corporate_account_by_obj_id($txn->id, 'transactions');
}
if(!empty($ca) && isset($ca->id) && !empty($ca->id)) {
$sub_users = $ca->sub_users();
foreach($sub_users as $user) {
$sub_user_ids[] = $user->ID;
}
}
}
$sub_user_ids = array_unique($sub_user_ids);
}
}
if(!empty($sub_user_ids)) {
echo '<h2>Your Sub Account Users</h2><ul>';
foreach($sub_user_ids as $user_id) {
$user = new MeprUser($user_id);
echo '<li>' . $user->user_login . '</li>';
}
echo '</ul>';
}
@Dabalina
Copy link

Is there a way to work this in reverse?

Basically to check to see if a user is a Sub Account and then look for the parent Corporate Account ID, and I am stuck there, I'm trying to figure out how to query all the Corporate Accounts get the matching ID and find the main user ID associated with the Corporate account.

@cartpauj
Copy link
Author

cartpauj commented Feb 11, 2021

@Dabalina you can get the user's meta global $user_ID; $mpca_id = get_user_meta($user_ID, 'mpca_corporate_account_id, true);

That will return a Corporate account ID (or nothing).

Once you have the corporate account ID, you can query the wp_mepr_corporate_accounts for that id to get the parent user ID. SELECT user_id FROM wp_mepr_corporate_accounts WHERE id = "$mpca_id"

@Dabalina
Copy link

Thank you! That worked perfectly!

@renatofrias26
Copy link

Hey mate, that is great. Thanks for sharing.
How hard would if be to adapt that to add a custom Role for each sub account? Basically I would like to have different capabilities for corp account vs sub account.
Cheers!

@upsited
Copy link

upsited commented Oct 26, 2021

@renatofrias26 I am trying to set a different role for sub accounts as well. Have you found a solution for this?

@mrkarstrom
Copy link

Hey mate, that is great. Thanks for sharing. How hard would if be to adapt that to add a custom Role for each sub account? Basically I would like to have different capabilities for corp account vs sub account. Cheers!

There is a Wordpress plugin for this called Members: https://memberpress.com/plugins/members

@cbarretta12
Copy link

I recently needed to loop through sub users for memberpress corporate accounts and landed here. But I could not get a variation of the functionality above to work. According to memberpress support:

"The MPCA_Corporate_Account::find_corporate_account_by_obj method does not return subaccounts.

To get sub-users from the parent account ID you can use the code snippet below:

global $wpdb;
$mepr_db   = MeprDb::fetch();
$mpca_db   = MPCA_Db::fetch();
$parent_id = 11; // Parent corporate user ID

$users_query = $wpdb->prepare( "SELECT user_id FROM {$mepr_db->transactions} WHERE txn_type = %s 
AND corporate_account_id = (SELECT id FROM {$mpca_db->corporate_accounts} WHERE user_id = %d)", 'sub_account', $parent_id );
$user_ids    = $wpdb->get_col( $users_query );

However, this query returns an empty array for some reason. I had to break the query into two parts and first query the corporate account ids. Using that, loop through and query the individual corporate account ids for sub users and then get unique values.

The below works for me. You'll need to get the parent corporate account user ID. I access it through the $sub in the mepr_subscription_transition_status action hook ($sub->user_id).

global $wpdb;
$mepr_db   = MeprDb::fetch();
$mpca_db   = MPCA_Db::fetch();
$parent_id = ''; // Parent corporate user ID

$corporate_account_ids_query = $wpdb->prepare( "SELECT id FROM {$mpca_db->corporate_accounts} WHERE user_id = %d", $parent_id );
$corporate_account_ids = $wpdb->get_col( $corporate_account_ids_query );

$sub_user_ids = [];
foreach ( $corporate_account_ids as $corporate_account_id ) {
	$sub_users_query = $wpdb->prepare( "SELECT user_id FROM {$mepr_db->transactions} WHERE txn_type = %s 
					    AND corporate_account_id = %d", 'sub_account', $corporate_account_id );
	$sub_user_ids_array = $wpdb->get_col( $sub_users_query );
        $sub_user_ids = array_merge( $sub_user_ids, $sub_user_ids_array );
}

if ( !empty( $sub_user_ids ) ) {
	$sub_user_ids = array_unique( $sub_user_ids );
        foreach ( $sub_user_ids as $sub_user_id ) {
                // Do what you need
        }
}

Hope it helps!

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