Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Drop down selection determines recipient using Gravity Forms notification filter and ACF Custom Fields WordPress

  • REFUNDED

I have a form that contains a select menu that is being dynamically populated using custom fields from repeater rows. The dynamic population of the select menu totally works. However, I then want the selection to be tied to a different email address (a custom field in the same repeater) so that the selection determines which email address to send a notification of the form submission.

The repeater name is "partners" and each row has the following fields:
"partners_country", for the dynamic population
"partners_email", for the email address that should be tied to that select option and used to send the notification

Below I am going to share the code I've used for the dynamic population, which does work. And then the code for the notification filter, which does not work. I am looking for some assistance to make that second block of code do what I want it to do.

What that second block of code is doing is that it it connecting to ACF in that it will send a notification but it's using the email of that very last row, regardless of which option has been selected in the select menu.

If it helps, I was trying to reference this code, where someone is doing this based on WP users, rather than ACF fields:
https://joshuadnelson.com/user-dropdown-list-custom-notification-routing-gravity-forms

Dynamic Population Code (working)


add_filter('gform_pre_render_1', 'dynamic_populate'); // Where 1 is GF ID
function dynamic_populate($form) {
foreach($form['fields'] as &$field){
if(strpos($field['cssClass'], 'region-field') === false)
continue;

global $post;
$id = $post->ID;

global $wpdb;
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
WHERE post_id = %d AND meta_key LIKE %s
",
$id,
'partners_%_partners_country'
// repeater-name_%_field-name
));

$choices = array(array('text' => 'Select a Region', 'value' => ' '));

if($rows){
foreach($rows as $row) {
preg_match('_([0-9]+)_', $row->meta_key, $matches);

$meta_key = 'partners_' . $matches[0] . '_partners_country';
$valueText = get_post_meta($post->ID,$meta_key,true);
$value = str_replace(' ', '-', strtolower($valueText));

$choices[] = array('text' => $valueText, 'value' => $value);
}
}
$field->choices = $choices;
}
return $form;
}


Notification Filter Code (not working)


add_filter('gform_notification_1', 'contact_email_notification', 10, 3);
function contact_email_notification($notification, $form , $entry) {

foreach($form['fields'] as &$field) {
if($field['type'] != 'select' || strpos($field['cssClass'], 'region-field') === false )
continue;

$field_id = (string) $field['id'];

global $post;
$id = $post->ID;

global $wpdb;
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
WHERE post_id = %d AND meta_key LIKE %s
",
$id,
'partners_%_partners_email'
// repeater-name_%_field-name
));

if($rows){
foreach($rows as $row) {
preg_match('_([0-9]+)_', $row->meta_key, $matches);
$meta_key = 'partners_' . $matches[0] . '_partners_email';
$email_to = get_post_meta($post->ID,$meta_key,true);
}
}
}
if (!empty($email_to)) {
$notification['to'] = $email_to;
}
return $notification;
}

Answers (1)

2021-06-17

Hariprasad Vijayan answers:

Hi there,

if ($rows) {
foreach ($rows as $row) {
preg_match('_([0-9]+)_', $row->meta_key, $matches);
$meta_key = 'partners_' . $matches[0] . '_partners_email';
$email_to = get_post_meta($post->ID, $meta_key, true);

// $notifcation[] = array('value' => $email_to);
}
}

Here I can see that you are assigning email id to $email_to in a loop, so $email_to will have the last email id when the loop end.

if(!empty($user_id)) {
$email_to = get_the_author_meta('user_email', $user_id);
}

And here i can see some condition to assign email id to $email_to, this condition won't work because $user_id is not defined anywhere. This code can remove from here.

Solution would be something like this

if( your-condition){
$email_to = get_post_meta($post->ID, $meta_key, true);
}

It's hard to provide the condition from the given details. Please let me know if you need any more help on this.

Kind regards,
Hariprasad


David Holtz comments:

There was some non-relevant code that was in that block that I have edited and removed so the above has been updated.

However, if you take a look at the first block of code, you can see how I am using get_post_meta to be able to work with the ACF custom field.

What is missing here is some way to tie what is being defined inside foreach($rows as $row) and then trying to tie that to the select menu option. That is what I can't figure out.


David Holtz comments:

If it helps, I was trying to reference this code, where someone is doing this based on WP users, rather than ACF fields:
https://joshuadnelson.com/user-dropdown-list-custom-notification-routing-gravity-forms


Hariprasad Vijayan comments:

I am assuming the meta_keys would be like partners_xyz_partners_country and partners_xyz_partners_email. And you are passing meta_value of partners_xyz_partners_country to the form submission. My doubt is how the meta value can relate with the email meta key partners_xyz_partners_email? Is this meta keys and values are unique for each post?


David Holtz comments:

I am confident that this is just a few lines of code away from working.

Everything comes down to two things:

The gform_notification filter:
https://docs.gravityforms.com/gform_notification/

The $entry object:
https://docs.gravityforms.com/entry-object/

The goal is to use $entry, which is the selected option, and tie it to the email.


Hariprasad Vijayan comments:

I agree, few line of code would solve the issue.

In your second function if you add like this
$field_id = (string) $field['id'];
$slected_region = $entry[ $field_id ];

you would probably get the selected value from the dropdown. No my question is where the email id is stored? and how the value of selected region is related with it?

Can you show a screenshot of database tables where the meta value for region and email address stored?


David Holtz comments:

How ACF stores data in the wp_postmeta table is not going to be helpful here. A better way to look at this is the repeater name value is 'partners'. The ID number attached to 'partner_country' is always the same as the FIELD ID associated with 'partner_email'. If ID 1 is United States, FIELD ID 1 is the email address for United States. If ID 2 is Canada, FIELD ID 2 is the email address for Canada.


David Holtz comments:

Anyway, as far as I can tell, this line is what is reaching in to the db to find the field value:

$meta_key = 'partners_' . $matches[0] . '_partners_email';

The problem is saving that value and applying it to the selection.


Hariprasad Vijayan comments:

I am available here https://meet.google.com/dhi-abwr-vct, would like a screen share to see if i can help you to solve this.