Support

Account

Home Forums Add-ons Repeater Field Using gform_notification filter with ACF repeater field value

Unread

Using gform_notification filter with ACF repeater field value

  • 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

    The dynamic population works, but the notification filter doesn’t. It always grabs the value/email address of the very last row of the repeater, regardless of which option has been selected in the select menu.

    I feel like I’m a few lines of code from this working, but the issue is using $entry to somehow tie the selection to the email address through an array or something.

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

    I also found this, but I wsn’t able to adapt it to my needs cause I am not smart enough: https://gist.github.com/tmoitie/9808555

    This is the code that does work:

    // Populate GF with ACF Repeater
    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;
    }

    This is the code that doesn’t work:

    // Route to user address from drop down list, update the '1' to the ID of your form
    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;
    
    		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
    		));
    
    		// $field_id = (string) $field['id'];
    		// $post_id = $entry[ $field_id ];
    
    		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;
    }
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.