Skip to content

Instantly share code, notes, and snippets.

@webaware
Created April 21, 2022 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webaware/ccd1e4f09d38cf220ab033cf4dad963c to your computer and use it in GitHub Desktop.
Save webaware/ccd1e4f09d38cf220ab033cf4dad963c to your computer and use it in GitHub Desktop.
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