Reputation: 2456
I need to grab data of entry after post submit, i.e:
function update_campaign_amount($entry, $form) {
$donate_to_personal_camp_form = get_option('als_donate_to_personal_camp_form');
$main_campaign_form = get_option('pb_main_campaign');
if ( $form['id'] == $donate_to_personal_camp_form ) {
$campaign_id = $entry['55'];
$user_donation = $entry['4'];
$total_donations = get_post_meta($campaign_id, 'campaign_current_amount', true);
if (is_numeric($user_donation)) {
update_post_meta($campaign_id, 'campaign_current_amount', $total_donations + $user_donation);
}
}
}
add_action( 'gform_after_submission', 'update_campaign_amount', 10, 2 );
As you can see in the code the two variable $campaign_id
and $user_donation
gets the value of a specific entries, it works but it doesn't good enough, because today the number of the entry is 55, tomorrow it could be something else.. Is there any other options to get entry value? For example, I have only one field on that form with the type "total", and I need it's value, so I want to do something like:
$entry['type'] == 'numer'
But I could find anyway to get entry data without the use of absolute numbers. Anyone know more flexible way?
Upvotes: 2
Views: 2343
Reputation: 526
$entry will contain an associative array of all the Field IDs and values for the current form submission. So if your Total field has an ID of 20 you can access the total amount for the current submission like this: $entry['20']. The IDs of the fields don't change so $entry['20'] will always return the total. Further details here: https://www.gravityhelp.com/documentation/article/entry-object/
If you have lots of different forms and the Field IDs are different for each form then you can use this:
$fields = GAPI::get_fields_by_type( $form, $type );
So if you know that that you only have one number field you can get the ID like this:
$number_fields = GAPI::get_fields_by_type( $form, 'number' );
$my_number_field = $number_fields[0];
$field_id = $my_number_field->id;
Upvotes: 2