Home / Archive / MemberPress: Delete Transaction and User Account When Sub-Account is Removed
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Delete Transaction and User Account When Sub-Account is Removed

Adding this code snippet will automatically delete user account and related transaction when a sub-account is removed from the Corporate Account holder’s Sub-Accounts list.

Note: The code snippet checks if the sub-account user has any other active memberships before deleting the transaction and the user account. If the sub-account user has any other active subscription, the user will not be deleted.

Code Preview
php
<?php
add_action('mpca_remove_sub_account', function($transaction_id) {
    $txn = new MeprTransaction($transaction_id);
    $user_id = $txn->user_id;
    // Get MeprUser object for the user
    $user = new MeprUser($user_id);
    // Get all subscriptions for the user
    $active_subs = $user->active_product_subscriptions('ids', true); // Get active subscription IDs
    // Check if there are active subscriptions
    if (! empty($active_subs)) {
        error_log("Cannot delete user ID: {$user_id}. They have active subscriptions.");
        return; // Abort the deletion if the user has active subscriptions
    }
    // Optional: Log the operation for record-keeping
    error_log("Deleting user ID: {$user_id} and associated transaction.");
    // Proceed with deletion of the transaction and user
    $txn->destroy();
    wp_delete_user($user_id);
});

Comments

Add a Comment