Skip Navigation

[Resolved] E-Mail Alert

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted his users to be able to subscribe to a notification type. Lets say the user has a Job type and wants to notify user when they subscribe to a notification of a particular job type.

Solution:

This can be done by creating a secondary CPT for the notifications where the user can create the notification type they want for a job.

So on this notification cpt it should have attributes from the Jobs CPT that can be used to match a notification for a Job type. The notification CPT must also have an email field.

Then you can use this code below to send an email to a notification post based on a matching custom field criteria.

//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        $job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
        $job_stelle = (types_render_field( 'stelle-ist-geeignet-fur', array( 'id' => ''.$post_id.'') ));
        $notifications = get_posts('post_type=job-alarm');
        foreach ($notifications as $notification) {
            $email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
            $notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
            $notification_stelle = (types_render_field( 'stelle-ist-geeignet-fur-alarm', array( 'id' => ''.$notification->ID.'') ));
            if ( $job_type == $notification_type && count(array_intersect(explode(",",$job_stelle), explode(",",$notification_stelle))) > 0) {
                wp_mail($email, 'Dein Job Alarm: '.get_the_title($post_id), get_permalink($post_id)."HTML CODE CAN GO HERE");
            }
        }
    }
}
This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 61 replies, has 2 voices.

Last updated by SteffenM1628 6 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1089522

hey shane, great you understand my needs.
just tested the function.

if ( $job_type == $notification_type && count(array_intersect($job_stelle,  $notification_stelle)) > 0) {

the code does not check if a match to the job exists. it just proofs if there is more than 0 attributes selected.
this is how the function works now:
e.g.
the alarm has attribute A and the job has attribute A the mail triggers. Excellent ? one is matching
the alarm has attribute A and the job has attribute A, B the mail triggers. Excellent ? at least one is matching
the alarm has attribute A and the job has attribute B the mail triggers. Not good ;-( cause nothing is matching

can this work?:

if ( $job_type == $notification_type && count(array_intersect($job_stelle == $notification_stelle)) > 0) {
#1089542

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

I tried the last case here "the alarm has attribute A and the job has attribute B the mail triggers. Not good ;-( cause nothing is matching"

The email isn't being sent when the Job checkboxes and the notification checkboxes do not have intersecting selection.

As you can see i created some test jobs for my notification and none of those sends me an email.

Thanks,
Shane

#1089546

yes because i just tested this ;-):

if ( $job_type == $notification_type && count(array_intersect($job_stelle == $notification_stelle)) > 0) {

Now everything is back on

if ( $job_type == $notification_type && count(array_intersect($job_stelle, $notification_stelle)) > 0) {

could you please try again and you will get mails (sometimes its up to five minutes)

#1089574

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

Perhaps i'm still not understanding the logic.

This line of code will only work under this circumstance

if ( $job_type == $notification_type && count(array_intersect($job_stelle, $notification_stelle)) > 0) {

If the Job type is equal to the notification type AND at least 1 of the notifications stelle matches what is on the job. If those 2 conditions are not met then the email will not be sent.

But you're saying the email is sent when the Notification type and Job type is matching and there are no matching job and notification stelle . This is pretty much not possible to happen with this like of code because both sides must be true if the code is to work.

Thanks,
Shane

#1089592
shane 4.png
shane 3.png
shane 2.png
Shane 1.png

hey shane,

you are absolute right. the function does work. there is only one little problem in the last part of the function.

"If the Job type is equal to the notification type" (this part works fine/ so no problems here)
AND "at least 1 of the notifications stelle matches what is on the job". (this section is not right: it deos not check if there is a match - it only checks if one or more things are selected.

i have shot some screenshots to explain it more in detail 🙂
Hope this helps.

cheers

#1089641

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

So I was able to find the issue as to why it wasn't working.

But its a little more complex that i had imagined and this is because of how the fields are stored. It will take some time for me to go through and create the final solution so you will see alot of jobs created on your site.

Thanks,
Shane

#1090276

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

This may take some time as the issue that i'm having right now is that the custom checkbox uses a multidimensional array.

I need to break this array up to get out the values and rebuild it into a single dimensional array that can be compared using the default php function.

#1090430

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

Could you try now.

This issue should be resolved.

Thanks,
Shane

#1090431

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

The correct code now is.

//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        $job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
        $job_stelle = (types_render_field( 'stelle-ist-geeignet-fur', array( 'id' => ''.$post_id.'') ));
        $notifications = get_posts('post_type=job-alarm');
        foreach ($notifications as $notification) {
            $email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
            $notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
            $notification_stelle = (types_render_field( 'stelle-ist-geeignet-fur-alarm', array( 'id' => ''.$notification->ID.'') ));
            if ( $job_type == $notification_type && count(array_intersect(explode(",",$job_stelle), explode(",",$notification_stelle))) > 0) {
                wp_mail($email, 'Dein Job Alarm: '.get_the_title($post_id), get_permalink($post_id)."HTML CODE CAN GO HERE");
            }
        }
    }
}

#1092325

Hey shane,

everything seems to work fine now.

Thank you soooo uch for going the extra mile with that.
Could you give me an example of adding a new mulstiselect field to the function?
Because you add same more code un the childs function.

This would be great. So i am able to extend the function to my needs.

cheers

#1093116

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

You mean if you wanted to add another field ?

So as per usual you will do this.

//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        $job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
        $job_stelle = (types_render_field( 'stelle-ist-geeignet-fur', array( 'id' => ''.$post_id.'') ));

// this is a new field
        $job_stelle_new_field = (types_render_field( 'add the field slug here', array( 'id' => ''.$post_id.'') ));

        $notifications = get_posts('post_type=job-alarm');
        foreach ($notifications as $notification) {
            $email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
            $notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
            $notification_stelle = (types_render_field( 'stelle-ist-geeignet-fur-alarm', array( 'id' => ''.$notification->ID.'') ));
//this is the new field
            $notification_stelle_new_field = (types_render_field( 'add field slug here', array( 'id' => ''.$notification->ID.'') ));

            if ( $job_type == $notification_type && count(array_intersect(explode(",",$job_stelle), explode(",",$notification_stelle))) > 0 $$ add your comparison here ) {
                wp_mail($email, 'Dein Job Alarm: '.get_the_title($post_id), get_permalink($post_id)."HTML CODE CAN GO HERE");
            }
        }
    }
}

Please let me know if this is clear.

Thanks,
Shane

#1093142

i ask because you added more code:

// Add Shortcode
function get_custom_field( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'id' => '',
			'field' => '',
		),
		$atts
	);

	$value = (types_render_field( 'stelle-ist-geeignet-fur-alarm', array( 'id' => '533') ));
	$value2 = (types_render_field( 'stelle-ist-geeignet-fur', array( 'id' => '547') ));
	//var_dump(array_intersect($value[0],$value2[0]));
// 	$test = do_shortcode("[types id='". 533 ."' field='stelle-ist-geeignet-fur-alarm' separator=', '][/types]");
// 	$test2 = do_shortcode("[types id='". 547 ."' field='stelle-ist-geeignet-fur' separator=', '][/types]");
// 	$flattern = array();
// 	foreach ($value as $key => $value){ 
//             $new_key = array_keys($value); 
//             $flattern[] = $value[$new_key[0]]; 
//         } 
	
	//var_dump(explode(",",$value2));
	//var_dump($value);
	//var_dump(array_intersect(explode(',',$value),explode(',',$value2)));
// 	// 	foreach($value as $v){
// 			var_dump(array_values($v));
// 		append$value_array .= array_values($v); 
// 	}
// 	var_dump(explode(",",$test));
// 		var_dump(explode(",",$test2));
    //var_dump($value_array);
// 	var_dump(array_intersect(explode(",",$test),explode(",",$test2)));
 }
add_shortcode( 'get_custom_field', 'get_custom_field' );

do i have to extend this, too?

$value = (types_render_field( 'stelle-ist-geeignet-fur-alarm', array( 'id' => '533') ));
$value2 = (types_render_field( 'stelle-ist-geeignet-fur', array( 'id' => '547') ));
$value3 = (types_render_field( 'new-field-slug', array( 'id' => '548') ));
$value4 = (types_render_field( 'new-field-slug-alarm', array( 'id' => '549') ));

cheers

#1093220

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

My apologies. I was actually to remove this because it was for testing purposes.

I created a shortcode to see if I could manually print out the values. I was able to and thats how I knew that the format of the checkboxes were a multidimensional array. You can actually remove it because its not needed.

Thanks,
Shane

#1094111

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

If there are no further queries on this could you mark this ticket as resolved ?

Thanks,
Shane

#1099321

Hey Shane,

Thank you so much for helping me out.
Some final questions:

1. can i use the custom code on other sites where i use toolset or do i habe to make some adjustments?

2. in the email we have the post titel from the job displayed. Can we also display the job alarm post title, too?

Thank you! ?