Skip Navigation

[Resolved] Split: Send a different notification email after submitting based on whether or not the field is selected

This support ticket is created 3 years, 5 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 7 replies, has 2 voices.

Last updated by matthiasV-2 3 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#2126527

Pass this ticket to Waqar, *unless you can offer a valuable solution step-by-step before Waqar comes back from vacation this friday*.

Hi Waqar,

On this page hidden link we have a 'select field' 'Ik wil het aanbod gratis bekijken'.

2. We want to send a different notification email after submitting based on whether or not the field is selected.

For example 1 and 2:

If field 'Ik wil het aanbod gratis bekijken' is selected, the submit button goes to page A and notification A is sent to the user & admin.
If field 'Ik wil het aanbod gratis bekijken' is not selected, the submit button goes to page B and notification B is sent to the user & admin.

#2126689

Hi,

During testing on my website, I was able to make this work using the "cred_notification_recipients" filter:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

For example, the ID of the user form in question is "18888" and suppose that I want to send a notification named "Notification A", if that checkbox is checked, and the "Notification B", if it is not checked.

The code, in this case, would look like this:


add_filter('cred_notification_recipients', 'modify_recipients_user_form_18888', 10, 4);
function modify_recipients_user_form_18888($recipients, $notification, $form_id, $post_id) {
	// if specific form
	if ($form_id == 18888) {
		// if notification is named "Notification A"
		if ( isset($notification['name']) && 'Notification A' == $notification['name'] ) {
			// if the checkbox field is not checked remove all notification recipients
			if(!isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
				$recipients = array();
			}
		}
		// if notification is named "Notification B"
		if ( isset($notification['name']) && 'Notification B' == $notification['name'] ) {
			// if the checkbox field is checked remove all notification recipients
			if(isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
				$recipients = array();
			}
		}
	}
	return $recipients;
}

The code checks for the particular form and form notification and if it is the case where we don't want to send the notification, it removes all the recipients which stop the sending of that email notification.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2126993

Hi Waqar

We have setup the following custom code:

add_filter('cred_notification_recipients', 'modify_recipients_user_form_18888', 10, 4);
function modify_recipients_user_form_18888($recipients, $notification, $form_id, $post_id) {
// if specific form
if ($form_id == 18888) {
// if notification is named "Nieuwe gebruiker geregistreerd"
if ( isset($notification['Nieuwe gebruiker geregistreerd']) && 'Nieuwe gebruiker geregistreerd' == $notification['Nieuwe gebruiker geregistreerd'] ) {
// if the checkbox field is not checked remove all notification recipients
if(!isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
$recipients = array();
}
}
// if notification is named "Nieuwe gebruiker geregistreerd promo"
if ( isset($notification['Nieuwe gebruiker geregistreerd promo']) && 'Nieuwe gebruiker geregistreerd promo' == $notification['Nieuwe gebruiker geregistreerd promo'] ) {
// if the checkbox field is checked remove all notification recipients
if(isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
$recipients = array();
}
}
}
return $recipients;
}

For the form "Nieuwe gebruiker registreren DEFINITIEF (ID: 18888)" we have created 2 different notifications:

1. "Nieuwe gebruiker geregistreerd". This notification should be sent IF the user did NOT check "'Ik wil het aanbod gratis bekijken".
2. "Nieuwe gebruiker geregistreerd Promo". This notification should be sent IF the user did check "'Ik wil het aanbod gratis bekijken".

HOWEVER during testing we received BOTH the emails upon registration whether if we did or did not check 'Ik wil het aanbod gratis bekijken".

What is going wrong here?

#2128603

Waqar

Can we solve these issues now?

#2128883

Thanks for sharing the update.

I'm afraid, the custom code was modified incorrectly and you only needed to change the notification names "Notification A" and "Notification B". But, the code on your website also changed the array keys "$notification['name']":


add_filter('cred_notification_recipients', 'modify_recipients_user_form_18888', 10, 4);
function modify_recipients_user_form_18888($recipients, $notification, $form_id, $post_id) {
    // if specific form
    if ($form_id == 18888) {
        // if notification is named "Nieuwe gebruiker geregistreerd"
        if ( isset($notification['Nieuwe gebruiker geregistreerd']) && 'Nieuwe gebruiker geregistreerd' == $notification['Nieuwe gebruiker geregistreerd'] ) {
            // if the checkbox field is not checked remove all notification recipients
            if(!isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
                $recipients = array();
            }
        }
        // if notification is named "Nieuwe gebruiker geregistreerd promo"
        if ( isset($notification['Nieuwe gebruiker geregistreerd promo']) && 'Nieuwe gebruiker geregistreerd promo' == $notification['Nieuwe gebruiker geregistreerd promo'] ) {
            // if the checkbox field is checked remove all notification recipients
            if(isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
                $recipients = array();
            }
        }
    }
    return $recipients;
}

The correct code would look like this:


add_filter('cred_notification_recipients', 'modify_recipients_user_form_18888', 10, 4);
function modify_recipients_user_form_18888($recipients, $notification, $form_id, $post_id) {
    // if specific form
    if ($form_id == 18888) {
        // if notification is named "Nieuwe gebruiker geregistreerd"
        if ( isset($notification['name']) && 'Nieuwe gebruiker geregistreerd' == $notification['name'] ) {
            // if the checkbox field is not checked remove all notification recipients
            if(!isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
                $recipients = array();
            }
        }
        // if notification is named "Nieuwe gebruiker geregistreerd promo"
        if ( isset($notification['name']) && 'Nieuwe gebruiker geregistreerd promo' == $notification['name'] ) {
            // if the checkbox field is checked remove all notification recipients
            if(isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
                $recipients = array();
            }
        }
    }
    return $recipients;
}

This should do the trick.

#2128965

Waqar,

We have changed the code into the new one below, however during a test we received the email notification "Nieuwe gebruiker geregistreerd" while we checked 'Ik wil het aanbod gratis bekijken" and we should have received the email notification "Nieuwe gebruiker geregistreerd Promo".

add_filter('cred_notification_recipients', 'modify_recipients_user_form_18888', 10, 4);
function modify_recipients_user_form_18888($recipients, $notification, $form_id, $post_id) {
// if specific form
if ($form_id == 18888) {
// if notification is named "Nieuwe gebruiker geregistreerd"
if ( isset($notification['name']) && 'Nieuwe gebruiker geregistreerd' == $notification['name'] ) {
// if the checkbox field is not checked remove all notification recipients
if(!isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
$recipients = array();
}
}
// if notification is named "Nieuwe gebruiker geregistreerd promo"
if ( isset($notification['name']) && 'Nieuwe gebruiker geregistreerd promo' == $notification['name'] ) {
// if the checkbox field is checked remove all notification recipients
if(isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
$recipients = array();
}
}
}
return $recipients;
}

#2129671

If you'd like to send the notification named "Nieuwe gebruiker geregistreerd promo", when that checkbox field is checked, you should interchange the names of the notifications in the two conditional checks:


add_filter('cred_notification_recipients', 'modify_recipients_user_form_18888', 10, 4);
function modify_recipients_user_form_18888($recipients, $notification, $form_id, $post_id) {
    // if specific form
    if ($form_id == 18888) {
        // if notification is named "Nieuwe gebruiker geregistreerd promo"
        if ( isset($notification['name']) && 'Nieuwe gebruiker geregistreerd promo' == $notification['name'] ) {
            // if the checkbox field is not checked remove all notification recipients
            if(!isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
                $recipients = array();
            }
        }
        // if notification is named "Nieuwe gebruiker geregistreerd"
        if ( isset($notification['name']) && 'Nieuwe gebruiker geregistreerd' == $notification['name'] ) {
            // if the checkbox field is checked remove all notification recipients
            if(isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
                $recipients = array();
            }
        }
    }
    return $recipients;
}

The first condition will stop the notification if the field is not checked, which is what you want. When the field is not checked "Nieuwe gebruiker geregistreerd promo" notification won't be sent, but the "Nieuwe gebruiker geregistreerd" will be sent.

Similarly, the second condition will stop the notification if the field is checked. When the field will be checked, the "Nieuwe gebruiker geregistreerd" will not be sent, but the "Nieuwe gebruiker geregistreerd promo" will be sent.

#2132507

My issue is resolved now. Thank you Waqar!