Dynamically pull values from nested form using Gravity Forms & Gravity Wiz Nested Forms

So I've been going through a restructure build of an entire site, and part of that involved switching from Formidable Forms to Gravity Forms. We did this because we wanted to use the Nested Form feature, so that we could automate multiple attendees without having to create a new form for each.

Here's the problem - on our old site that did have a separate form per attendee via Formidable, we had a code using the Canvas API to send name + email info to Canvas and automatically register users for the online courses this company offers. In trying to convert sections of this code to work with my nested forms, I'm running into a snag:

  1. The main issue is that the value is being spit out as all of the information from the nested form entry, not by name/ email/ etc.

  2. The info is being spit out twice, perhaps because of the way the forms are structured? There are a couple calculations happening in the forms/ nested forms so I'm chalking it up to that.


      [1] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 212
                    [key] => Attendee Registration
                    [value] => 
    
                                            
                                            Name
                        Test Name
                                            Email Address
                        [email protected]
                                            Cell Phone
                        (333) 333-3333
                                            Would you like to receive text message reminders for this registration?
                        No
                                            Post-class notification is required for the following states, please identify if you will be using this class to fulfill any state license requirements:
                        N/A
                                            You'll receive a hard copy and digital certificate upon course completion. Additional options are available here:
                        
                                            All live classes include a hard copy manual and regulations. To join our effort to save paper, please also add any of the following options to take your books home:
                        
                    
                


                )

            [data:protected] => Array
                (
                    [id] => 212
                    [key] => Attendee Registration
                    [value] => 
    
                                            
                                            Name
                        Test Name
                                            Email Address
                        [email protected]
                                            Cell Phone
                        (333) 333-3333
                                            Would you like to receive text message reminders for this registration?
                        No
                                            Post-class notification is required for the following states, please identify if you will be using this class to fulfill any state license requirements:
                        N/A
                                            You'll receive a hard copy and digital certificate upon course completion. Additional options are available here:
                        
                                            All live classes include a hard copy manual and regulations. To join our effort to save paper, please also add any of the following options to take your books home:
                        
                    
                


                )

        )

Also: I was playing around with grabbing the ID of the main entry via [_gravity_form_linked_entry_id], and grabbing the nested info from that via [_gravity_form_lead].

The best I was able to get from that was this... so yeah kind of lost on how to progress here if anyone has any pointers! Thanks so much!

[data:protected] => Array
                (
                    [id] => 211
                    [key] => _gravity_forms_history
                    [value] => Array
                        (
                            [_gravity_form_cart_item_key] => 72201a9586fb30895b8fb5cac2a796b9
                            [_gravity_form_linked_entry_id] => 125
                            [_gravity_form_lead] => Array
                                (
                                    [form_id] => 1
                                    [source_url] => https://chcv2.flywheelstaging.com/product/air-monitoring-specialist-live/
                                    [ip] => 75.151.95.41
                                    [42.1] => Course Price
                                    [42.2] => $580.00
                                    [42.3] => 1
                                    [21] => 122
                                    [40.1] => Add-On Fees
                                    [40.2] => $0.00
                                    [40.3] => 1
                                )

                            [_gravity_form_data] => Array
                                (
                                    [id] => 1
                                    [bulk_id] => 0
                                    [display_title] => 
                                    [display_description] => 
                                    [disable_woocommerce_price] => no
                                    [price_before] => 
                                    [price_after] => 
                                    [disable_calculations] => no
                                    [disable_label_subtotal] => yes
                                    [disable_label_options] => yes
                                    [disable_label_total] => no
                                    [disable_anchor] => no
                                    [label_subtotal] => Course Fee
                                    [label_options] => Additional Attendees + Selected Options
                                    [label_total] => Attendee Registration + Add-Ons:
                                    [use_ajax] => no
                                    [enable_cart_edit] => no
                                    [enable_cart_edit_remove] => no
                                    [keep_cart_entries] => no
                                    [send_notifications] => no
                                    [enable_cart_quantity_management] => stock
                                    [cart_quantity_field] => 
                                    [update_payment_details] => yes
                                    [display_totals_location] => after
                                    [structured_data_override] => no
                                    [structured_data_low_price] => 
                                    [structured_data_high_price] => 
                                    [structured_data_override_type] => overwrite
                                )

                        )

                )

Update: Here's how I've incorporated the code from Rochelle's comment below, where I'm getting an error

add_action( 'woocommerce_thankyou', 'canvas_enroll', 20, 2 );

function canvas_enroll($orders) {
    $query = new WC_Order_Query( array(
        'orderby' => 'date',
        'order' => 'DESC',
        'return' => 'ids',
    ) );
    $orders = $query->get_orders();
    foreach($orders as $order){
        foreach ($order->get_items() as $item_id => $item_data) {
            if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"]) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"])){ 
                $linked_entry=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
                $entry_id = $linked_entry;
                $entry = GFAPI::get_entry( $entry_id );//id of Parent Gravity Form
                if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2']) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'])){
                    $linked_nested_value=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'];
                    $nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries
                    $child_entry_amt = substr_count($linked_nested_value, ",") + 1;
                    if ($child_entry_amt > 0){   
                        for ($n = 0; $n < $child_entry_amt; $n++) {
                            $entry_id_nest[$n]=$nested_value_array[$n];
                            $entry_nest[$n] = GFAPI::get_entry( $entry_id_nest[$n] ); //nested form entry
                            $name[$n]  = $entry_nest[$n]['12.3'].''.$entry_nest[$n]['12.6'];//replace 1.3 and 1.6 with nested field id of name
                            $email[$n] = $entry_nest[$n]['11']; //2 is the GF nested field id of email        
                        }
                    }
                }            
            }               
        }
    }
}

Solution 1:

Finally got this figured out! Something that was super helpful was to echo the item meta data ($value, in my case) for that to display all the ids and such, that's how I was able to figure out that I needed 21 in that ID for the child entries.

I'm not really sure why I had to switch to wc_get_order instead of wc_order_query, but it solved the errors I was getting.

function canvas_enroll($order_id) {

$order = wc_get_order( $order_id);
    
$order_id = array(
    'orderby' => 'date',
    'order' => 'DESC',
    'return' => 'ids',
);  
    
if(!empty($order) && isset($order)){
// Loop through order line items
foreach( $order->get_items() as $key => $value ){

    // get order item data (in an unprotected array)
             
            if(isset($value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"]) && !empty($value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"])){ 
                
                $linked_entry=$value->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
                $entry_id = $linked_entry;
                $entry = GFAPI::get_entry( $entry_id );//id of Parent Gravity Form              
                
                if(isset($value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['21']) && !empty($value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['21'])) { //21 was the id for my child form

                    
                    $linked_nested_value = $value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['21']; 
                    
                    $nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries
                    $child_entry_amt = substr_count($linked_nested_value, ",") + 1;
                    if ($child_entry_amt > 0){   
                        for ($n = 0; $n < $child_entry_amt; $n++) {
                            $entry_id_nest[$n]=$nested_value_array[$n];
                            $entry_nest[$n] = GFAPI::get_entry( $entry_id_nest[$n] ); //nested form entry
                            $firstname[$n]  = $entry_nest[$n]['12.3'];//replace 12.3 with nested field id of first name
                            $lastname[$n]  = $entry_nest[$n]['12.6'];//replace 12.6 with nested field id of last name
                            $email[$n] = $entry_nest[$n]['11']; //replace 11 with nested field id of email        
                        }
                    } 
                } 
            }         
        }
    }       
}