Skip to content

Commit e1cbc66

Browse files
authored
gw-consolidate-multiple-list-fields-to-single-list-field.php: Added snippet to consolidate multiple list fields to a single list field.
1 parent 9da9520 commit e1cbc66

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Gravity Wiz // Gravity Forms // Consolidate Multiple List Fields into a Single List Field
4+
* https://gravitywiz.com/
5+
*
6+
* Instruction Video: https://www.loom.com/share/8b45a92cf56249a982aa1aa6e1301778
7+
*
8+
* This snippet merges values from multiple list fields into a single list field.
9+
*/
10+
// Update "123" to your form ID.
11+
add_action( 'gform_post_submission_123', function ( $entry, $form ) {
12+
// Define source field IDs and target field ID.
13+
$source_field_ids = array( 1, 5, 4 );
14+
$target_field_id = 7;
15+
16+
$combined = array();
17+
18+
// Loop through source field IDs and merge their unserialized values.
19+
foreach ( $source_field_ids as $field_id ) {
20+
if ( isset( $entry[ $field_id ] ) && ! empty( $entry[ $field_id ] ) ) {
21+
$field_values = unserialize( $entry[ $field_id ] );
22+
if ( ! $field_values || ! is_array( $field_values ) ) {
23+
$field_values = array();
24+
}
25+
$combined = array_merge( $combined, $field_values );
26+
}
27+
}
28+
29+
// Re-index the combined array.
30+
$combined = array_values( $combined );
31+
32+
// Serialize the combined array and update the target field and entry.
33+
$finalSerialized = serialize( $combined );
34+
$entry[ $target_field_id ] = $finalSerialized;
35+
36+
GFAPI::update_entry( $entry );
37+
}, 10, 2 );

0 commit comments

Comments
 (0)