• Resolved intarwebsdeveloper

    (@intarwebsdeveloper)


    I’m trying to create a couple new custom Email Notifications by using this hook

    add_action("um_after_user_updated","um_custom_after_user_updated", 10, 3 );
    function um_custom_after_user_updated(  $user_id, $args, $to_update ){
         // Do something
    }

    I followed Champ Camba’s tutorial here – https://www.champ.ninja/2020/05/custom-email-notifications/ and was able to get 1 notification to work correctly.

    However, when I try to create multiple email notifications to check different user meta keys, all the functions are being fired when

    add_action("um_after_user_updated","custom_function_name", 10, 3 ) happens.

    For example, I created a notification that checks if a specific meta key has a specific value and it works fine. But when I tried to create another function to check for a different meta key, both of the functions are being fired and both email notifications are being sent even if the conditions for one of them are not being met.

    What’s the proper way to add multiple Email Notifications for different user meta keys so they don’t all fire every time

    add_action("um_after_user_updated","custom_function_name", 10, 3 ) is called?

    • This topic was modified 5 years, 1 month ago by intarwebsdeveloper. Reason: Forgot code formatting
    • This topic was modified 5 years, 1 month ago by intarwebsdeveloper. Reason: Fixed formatting
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @intarwebsdeveloper

    Could you please provide the codes with the meta key conditions?

    Regards,

    Thread Starter intarwebsdeveloper

    (@intarwebsdeveloper)

    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.

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @intarwebsdeveloper

    This action hook um_after_user_updated has 3 parameters:
    – $user_id,
    – $args,
    – $to_update

    I noticed that you are using the $user_id as $metakey which is returning the user_id of the current updating user. So I’m not sure what data are returned in the conditions that’s causing the issue:

    1. if( $metakey != $list_uncc_text ) {
    2. if( strlen($meta_key) < strlen($company_info) || strlen($meta_key) > strlen($company_info) ) {

    Regards,

    Thread Starter intarwebsdeveloper

    (@intarwebsdeveloper)

    @champsupertramp Thanks for pointing that out. I’ll get that corrected and see if that fixes the issue.

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @intarwebsdeveloper

    Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. 🙂

    Regards,

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Multiple Custom Email Notifications question’ is closed to new replies.