Skip to content

Instantly share code, notes, and snippets.

@KevinGraham-com
Created August 30, 2023 08:32
Show Gist options
  • Save KevinGraham-com/21606cbfeafbd7e92de886ad611012cd to your computer and use it in GitHub Desktop.
Save KevinGraham-com/21606cbfeafbd7e92de886ad611012cd to your computer and use it in GitHub Desktop.
WHMCS - Sync User and Client Email on Update
<?php
if (!defined('WHMCS')) {
exit('This file cannot be accessed directly');
}
add_hook('UserEdit', 5, 'sync_user_to_client');
add_hook('ClientEdit', 5, 'sync_client_to_user');
function sync_user_to_client($vars) {
// gather the data
$newmail=$vars['email'];
$oldmail=$vars['olddata']['email'];
// check to see if email address updated
if ($newmail != $oldmail) {
//find the client account
$command0 = 'UpdateClient';
$postData0 = array(
'clientemail' => $oldmail,
'email' => $newmail,
);
$results0 = localAPI($command0, $postData0);
if ($results0['result']=="success") {
logActivity('Sync User to Client Update - Client ID ' . $results0['clientid'] . ' updated email from (' . $oldmail . ') to ('.$newmail.')');
}
}
}
function sync_client_to_user($vars) {
// gather the data
$newmail=$vars['email'];
$oldmail=$vars['olddata']['email'];
// check to see if email address updated
if ($newmail != $oldmail) {
// find the user account
$command = 'GetUsers';
$postData = array(
'search' => $oldmail,
);
$results = localAPI($command, $postData);
$command2 = 'UpdateUser';
$postData2 = array(
'user_id' => $results['users'][0]['id'],
'email' => $newmail,
);
$results2 = localAPI($command2, $postData2);
if ($results2['result']=="success") {
logActivity('Sync Client to User Update - User ID ' . $results['users'][0]['id'] . ' updated email from (' . $oldmail . ') to ('.$newmail.')');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment