Hi there,
Here is how I managed to handle the issue:
- Added a generic hidden field in the form.
- I used the same method you used to add the country inside the field default value:
[cred_generic_field type='hidden' field='email-country']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-field name="wpcf-procurement-country" id='[wpv-post-param var="parent_rfq_id"]']"
}
[/cred_generic_field]
- Added a notification in the form settings and called it 'Country Email'.
- It will send an email when the form is submitted.
- I did not add any recipients there as we will do it dynamically via code.
- Added a custom code in Toolset named 'rfqnotification':
https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
- Here is the code:
add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
// Check the form ID to target
if ($form_id == 239) {
// Check notification name matches target notification
if ( isset($notification['name']) && 'Country Email' == $notification['name'] ) {
// Get the country name
$the_name = $_REQUEST['email-country'];
//Add proper address for each country
if ($the_name == 'Africa Alliance') {
$recipients[] = array(
'to' => 'to',
'address' => '----'
);
} else {
$recipients[] = array(
'to' => 'to',
'address' => '----',
);
}
}
}
return $recipients;
}
- That will generate dynamic email addresses and you need to update the addresses depending on the country name.
Code description:
It uses the 'cred_notification_recipients' hook to modify the notification recipient:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
It first checks to make sure the correct form ID is targeted, for you case it was 239
Then it checks if there is a notification set up for that form or not and target the notification. I you remember the notification I created was called 'Country Email' and that is used in the if statement to check.
Then we get the value of the generic hidden field that we created in the form using $_REQUEST['email-country'].
Next step is a series of If/else statements that provide a separate email for each country name.
I only added the Africa Alliance one and if you see in the address section I added ---, you just need to replace it with the email address that you want to send to for that country.
Thanks.