Skip to content

Instantly share code, notes, and snippets.

@DumahX
Created July 25, 2022 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DumahX/394a7887e0870fa8527a38af3e86d675 to your computer and use it in GitHub Desktop.
Save DumahX/394a7887e0870fa8527a38af3e86d675 to your computer and use it in GitHub Desktop.
Remove duplicate corporate accounts from the mpca_corporate_accounts table.
<?php
add_action('init', function() {
$users = MeprUser::all();
$ca_obj_ids = array();
$duplicates = array();
foreach($users as $user) {
$corporate_accounts = MPCA_Corporate_Account::get_all_by_user_id($user->ID);
if(!empty($corporate_accounts)) {
foreach($corporate_accounts as $ca_rec) {
$corporate_account = new MPCA_Corporate_Account();
$corporate_account->load_from_array($ca_rec);
// If this is a duplicate corporate account with no sub accounts used. Add it as a duplicate.
if(in_array($corporate_account->obj_id, $ca_obj_ids) && $corporate_account->num_sub_accounts_used() <= 0) {
$duplicates[] = $corporate_account;
continue;
}
$ca_obj_ids[] = $corporate_account->obj_id;
}
}
}
// If there were duplicates, remove them as a corporate account.
if(!empty($duplicates)) {
foreach($duplicates as $duplicate) {
$duplicate->destroy();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment