Skip Navigation

[Resolved] Select/Include an email recipient conditionally of the submitted data

This support ticket is created 3 years, 11 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by stephaneL-3 3 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1882935

Tell us what you are trying to do?
I am trying to set the email recipient of a Form submission notification conditionally based on the relationship association.

However I have included the field to use a recipient depending on the data selected for the relationship. (It is a directory and the form is submitting an entry, each entry belongs to a Zone/ via relation (Parent))

[cred_show_group if="( $(@departement-region-project.parent) eq '1596' )" mode="fade-slide"]

[cred_generic_field type='email' field='EmailYarget']
{
"required":0,
"validate_format":0,
"default":"test@test.org"
}
[/cred_generic_field]
[/cred_show_group]

It shows on the screen no problem, however when it comes to select it as a Recipient of a notification when I select: " Send notification to an email specified in an email field included in the form:" The (Choose and email) list just doesn't give me any option in the dropdown I refresh over and over but still it just doesn't show anything?

Any idea?

Is there any documentation that you are following?
Conditional recipient: https://toolset.com/forums/topic/need-filter-send-cred-emails-only-if/
Showing Parent relationship related field data: https://toolset.com/forums/topic/using-relationships-in-conditions-in-a-conditional-group-in-a-form/

What is the link to your site?

#1883879

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

The thing is that to send email notification to the generic email field you added to your form, you will require to use the Tooslet Form's notification hook:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

For example:

add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
 
    // Check notification name matches target notification
    if ($form_id==9999 and  isset($notification['name']) && $notification['name'] == 'Content submitted') {
 
        $email = $_REQUEST['EmailYarget'];

        $recipients[] = array(
            'to'        =>  'to',
            'address'   => $email,
            'name'      =>  '',
            'lastname'  =>  ''
            );
    }
          
    return $recipients;
}

Where:
- Replace 9999 with your original form ID and replace the "Content submitted" with your original notification name you given.

You should add the above code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
OR
To the functions.php file of your current theme.

#1884351

Hello and thanks for your reply,

I understand what you mean. The idea is to send the notification to different email depending on a field where teh user select the Parent Zone.
The user is submitting a project for review, this project beloong to a Zone, depending on the zone I need to send a notification to different emails.
I have already the Email set against Each Zone in the Zone Post Type.
This Post Type is related to the Project CPT.
When the Project is submitted via Cred Form I need the Zone's Email to get sent the email too.

So that only the Zone Admin receive its project, not from the other Zone?

Makes Sense?
\
Can you assist me modifying th escript to include a condition to use the email of the zone where the project belongs?

Thanks for your time
Stephane

#1884701

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I will require to see how you setup the zone field with your Toolset form and then once I review that I will be able to guide you in the right direction.

Can you please share problem URL where you added the Toolset form from where user submit the project as well as access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1884823
Screenshot 2020-12-23 093626.jpg

Hey Minesh,

while you're on this form, could you please check the Date field listed down the form? I don't understand why I just noticed it is not showing. In looking it is Hidden via CSS?? I don't see why it used to show ok and the Field shortcode seems ok, I regenerate the field and same issue it won't show?

#1884869

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

We will address the issue one by one, so lets fix first your original issue.

You want to send the email notification to the email field value of the field email-administrateur-contact - correct?

#1884909

Hello,

Yes it is the field in the Zone CPT : email-administrateur-contact

((I understand thank you for looking into this)

stephane

#1884945

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now.

I've adjusted the code at "Custom Code" section of Toolset:

add_filter('cred_notification_recipients', 'func_modify_recipients', 10, 4);
function func_modify_recipients($recipients, $notification, $form_id, $post_id) {
  
    // Check notification name matches target notification
    if ($form_id==1656 and  isset($notification['name']) && $notification['name'] == 'New-project-admin') {
  
        $parent_id = $_REQUEST['@departement-region-project_parent'];
        $email = get_post_meta($parent_id,'wpcf-email-administrateur-contact',true);
 
        $recipients[] = array(
            'to'        =>  'to',
            'address'   => $email,
            'name'      =>  '',
            'lastname'  =>  ''
            );
    }
           
    return $recipients;
}

Where, we try to get first $parent_id and based on that $parent_id we get the associated email.

Can you please confirm it works as expected now.

#1884973

YES! Thank you Minesh, that is awesome 😉

Regarding the other issue would you prefer if I raise a new ticket or can you help me on this thread ?

Cheers
Stephane

#1884979

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes, please.

As per our support policy, we entertain only one question per ticket. This will help other users searching on the forum as well as help us to write correct problem resolution summery for the original issue.

#1884993

My issue is resolved now. Thank you!