Created
April 21, 2022 04:27
-
-
Save webaware/ccd1e4f09d38cf220ab033cf4dad963c to your computer and use it in GitHub Desktop.
adapt Gravity Forms notification rules based on country in an Address field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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