Here’s one email notification that checks the string length of the current fields meta value. If the updated field has more or less, then the notification should fire.
function csc_custom_email_notifications_company_desc( $notifications ){
$notifications['user_updated_company_desc'] = array(
'key' => 'user_updated_company_desc',
'title' => __( 'A Supplier has updated their Company Description','um-groups' ),
'subject' => 'A Supplier has updated their Company Description ',
'body' => '',
'description' => __('Sends an email if a Supplier has updated their Company Description','ultimate-member'),
'recipient' => 'admin',
'default_active' => true
);
return $notifications;
}
add_filter( 'um_email_notifications', 'csc_custom_email_notifications_company_desc', 10, 1 );
add_action('um_after_user_updated','csc_notify_when_user_updates_company_desc',10, 3);
function csc_notify_when_user_updates_company_desc( $meta_key,$user, $args ){
// logged in member
$user_id = $user->ID;
um_fetch_user( $user_id );
$recipient = 'admin@website.tld';
$profile_name = um_user('member_contact_2');
// viewed member profile
$profilepage_id = um_profile_id();
um_fetch_user($profilepage_id);
$stock_number_value = um_user('stock_no');
// check stock meta value, if has s, get users that match stock value and get these users' meta data
if (stripos($stock_number_value, 's') !== false) {
$args = array(
'meta_key' => 'stock_no',
'meta_value' => $stock_number_value,
'role' => 'um_supplier',
);
$users = get_users( $args );
if ($users) {
foreach ( $users as $user ) {
$member_id = $user->ID;
$member_name = $user->first_name;
$company_info = $user->company_info;
}
}
}
$member_profile_url = home_url( '/' ).'user/'.$member_id.'/';
// if current meta key data has more or less string length, send email
if( strlen($meta_key) < strlen($company_info) || strlen($meta_key) > strlen($company_info) ) {
UM()->mail()->send( $recipient, 'user_updated_company_desc', array(
'plain_text' => 1,
'tags' => array(
'{profile_name}',
'{company_info}',
'{member_name}',
'{member_profile_url}',
),
'tags_replace' => array(
$profile_name,
$company_info,
$member_name,
$member_profile_url,
)
) );
}
}
Then here’s another function that checks a different meta key value:
function um_custom_email_notifications_cnn_news( $notifications ){
$notifications['user_updated_cnn_news'] = array(
'key' => 'user_updated_cnn_news',
'title' => __( 'A User has updated News settings','um-groups' ),
'subject' => '',
'body' => '',
'description' => __('Sends an email if the News settings are updated','ultimate-member'),
'recipient' => 'admin',
'default_active' => true
);
return $notifications;
}
add_filter( 'um_email_notifications', 'um_custom_email_notifications_cnn_news', 10, 1 );
add_action('um_after_user_updated','csc_notify_when_user_updates_cnn_news',10, 3);
function csc_notify_when_user_updates_cnn_news( $metakey, $user ){
$user_id = $user->ID;
um_fetch_user( $user_id );
$recipient = 'admin@website.tld';
// get meta keys
$profile_name = um_user('member_contact_2');
$list_uncc_text = um_user('list_uncc_text');
if( $metakey != $list_uncc_text ) {
// get first_name meta value of viewed profile
$profilepage_id = um_profile_id();
um_fetch_user($profilepage_id);
$stock_number_value = um_user('stock_no');
if (stripos($stock_number_value, 's') !== false) {
$args = array(
'meta_key' => 'stock_no',
'meta_value' => $stock_number_value,
'role' => 'um_supplier',
);
$users = get_users( $args );
if ($users) {
foreach ( $users as $user ) {
$member_id = $user->ID;
$member_name = $user->first_name;
}
}
}
$member_profile_url = home_url( '/' ).'user/'.$member_id.'/';
UM()->mail()->send( $recipient, 'user_updated_cnn_news', array(
'plain_text' => 1,
'tags' => array(
'{profile_name}',
'{member_name}',
),
'tags_replace' => array(
$profile_name,
$member_name,
)
) );
}
}
So when all this code is together. If I were to update only one of targeted meta keys in the profile, I get both email notifications for the meta keys even if I only updated one of them.