使用 Gravity Forms & Gravity Wiz Nested Forms 从嵌套表单中动态提取值

Posted

技术标签:

【中文标题】使用 Gravity Forms & Gravity Wiz Nested Forms 从嵌套表单中动态提取值【英文标题】:Dynamically pull values from nested form using Gravity Forms & Gravity Wiz Nested Forms 【发布时间】:2022-01-17 03:33:16 【问题描述】:

所以我一直在对整个网站进行重组构建,其中一部分涉及从强大的形式切换到重力形式。我们这样做是因为我们想使用嵌套表单功能,这样我们就可以自动化多个参与者,而不必为每个参与者创建一个新表单。

这就是问题所在 - 在我们通过 Formidable 为每位参与者提供单独表格的旧网站上,我们有一个代码使用 Canvas API 将姓名和电子邮件信息发送到 Canvas 并自动为该公司提供的在线课程注册用户。在尝试将此代码的部分转换为使用我的嵌套表单时,我遇到了一个障碍:

    主要问题是该值作为嵌套表单条目中的所有信息被吐出,而不是按名称/电子邮件/等。

    信息被吐出两次,可能是因为表单的结构方式?表单/嵌套表单中发生了一些计算,所以我将其记录下来。


      [1] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 212
                    [key] => Attendee Registration
                    [value] => 
    
                                            
                                            Name
                        Test Name
                                            Email Address
                        courses@email.com
                                            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
                        courses@email.com
                                            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:
                        
                    
                


                )

        )

另外:我正在尝试通过 [_gravity_form_linked_entry_id] 获取主条目的 ID,并通过 [_gravity_form_lead] 从中获取嵌套信息。

我能从中得到的最好的结果就是......所以如果有人有任何指示,我有点迷失了如何在这里取得进展!非常感谢!

[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
                                )

                        )

                )

更新:以下是我从 Rochelle 的评论中合并代码的方式,但出现错误

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        
                        
                    
                            
                           
        
    

【问题讨论】:

我不太确定我在看什么,但我想我做过类似的事情。这是带有重力形式附加组件的 woocommerce 产品吗?我只是在查看 woocommerce 元数据的 var_dump 吗? 嘿,Rochelle,这正是您所看到的!它是一款带有 GF 附加组件的 WC 产品。这里棘手的部分是还有一个嵌套表单插件,我无法从 GF 表单中的嵌套表单中获取特定字段。 【参考方案1】:

我将粘贴用于另一个项目的代码,我需要提取相同类型的数据,以防它让您走上正确的轨道。您必须用表单中的 id 替换数字:

$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]['1.3'].''.$entry_nest[$n]['1.6'];//replace 1.3 and 1.6 with nested field id of name
                        $email[$n] = $entry_nest[$n]['2']; //2 is the GF nested field id of email        
                    
                
                        
                       
    

【讨论】:

非常感谢!看起来很棒,很高兴明天试一试 通知我。 嘿,所以当我使用它时,我实际上遇到了一个错误 - 页面加载但有点不稳定的那种,它说“这个网站上有一个严重错误”。编辑:点击进入太快了哈哈,哎呀!我会将其中的内容添加到我的原始帖子中作为编辑。 是的,请随时准确发布您添加的内容,因为这是为了在函数中运行。 (您可以访问网站上的错误日志吗?)。无论哪种方式,您都必须进行一些调整才能使其正常工作,但我会快速扫描错误。同时,您可能需要在诸如“$linked_entry”之类的东西上运行 var_dump 以真正查看它返回的内容,看看您是否可以获取嵌套的表单条目值。 我将它添加到我的初始帖子中作为更新 :) 我刚刚用谷歌搜索了如何查看错误日志(你能告诉我我通常做前端吗哈哈)所以我会看看下次我在那里!谢谢!

以上是关于使用 Gravity Forms & Gravity Wiz Nested Forms 从嵌套表单中动态提取值的主要内容,如果未能解决你的问题,请参考以下文章

php Gravity Wiz // Gravity Forms //按表单名称获取表单ID

php Gravity Wiz // Gravity Forms //添加日期合并标记的格式选项

如何在 Wordpress Gravity Forms 中使用 PHP 创建条目

php Gravity Wiz // Gravity Forms Coupons //允许零金额优惠券

php Gravity Wiz // Gravity Forms //用户注册//通过电子邮件更新

css Gravity Wiz // Gravity Forms // CSS // Hide AJAX Spinner