Home / Archive / Get All the Sub-Accounts Data Under a Corporate Parent User
Duplicate Snippet

Embed Snippet on Your Site

Get All the Sub-Accounts Data Under a Corporate Parent User

This PHP snippet creates a WordPress shortcode that displays a list of sub-account users associated with the currently logged-in parent account with active subscriptions.

How to Implement and Use:

Add the Code:
Insert the provided PHP snippet into your website. You can achieve this by adding it to your theme’s functions.php file. Alternatively, you can use the WPCode plugin to manage snippets without directly editing theme files.

Use the Shortcode:
To display the list of sub-account users on any page, post, or widget, simply use the shortcode [sub_account_users] wherever you want the information to appear.

View the Results:
After adding the shortcode to your desired location, ensure you are logged in as a user who has active memberships with sub-users associated.
Visit the page or post containing the shortcode to see the dynamically generated list of sub-account user names.
If the user has no sub-user accounts, the output will display "No sub-account users found."

Code Preview
php
<?php
function display_sub_account_users() {
    $user = MeprUtils::get_currentuserinfo();
    $sub_user_ids = array();
    $output = '';
    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)) {
        $output .= '<h2>Your Sub Account Users</h2><ul>';
        foreach ($sub_user_ids as $user_id) {
            $user = new MeprUser($user_id);
            $output .= '<li>' . esc_html($user->user_login) . '</li>';
        }
        $output .= '</ul>';
    } else {
        $output .= '<p>No sub-account users found.</p>';
    }
    return $output;
}
add_shortcode('sub_account_users', 'display_sub_account_users');

Comments

Add a Comment