Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
adapt Gravity Forms notification rules based on country in an Address field
<?php
if (!defined('ABSPATH')) {
exit;
}
/**
* adapt notification rules based on country in Address field
* - set Send To email address to a trigger value
* - set Value to the address field ID
* - doesn't matter what the other values are
* @param array $notification
* @param array $form
* @param array $entry
*/
add_filter('gform_notification', function($notification, $form, $entry) {
// only if using email routing
if (rgar($notification, 'toType') === 'routing' && !empty($notification['routing'])) {
// check for trigger value, otherwise it's using regular email routing
$route = $notification['routing'][0];
if (rgar($route, 'email') === 'trigger@example.com') {
$field = GFAPI::get_field($form['id'], rgar($route, 'value'));
if ($field instanceof GF_Field && $field->type === 'address') {
$map = [
'Australia' => 'au@example.com',
'United States' => 'us@example.com',
'China' => 'cn@example.com',
'Brazil' => 'br@example.com',
];
// get Country value and use it to determine email address, with a fallback address
$value = rgar($entry, $field->id . '.6');
$email = $map[$value] ?? 'general@example.com';
$notification['toType'] = 'email';
$notification['toEmail'] = $email;
$notification['to'] = $email;
$notification['routing'] = [];
}
}
}
return $notification;
}, 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment