重力形式 - 获取没有条目号的条目数据

时间:2016-09-05 13:20:51

标签: php wordpress gravity-forms-plugin

我需要在提交后获取条目数据,即:

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

正如您在代码中看到的那样,两个变量$campaign_id$user_donation获取特定条目的值,它可以工作,但它不够好,因为今天的数量是条目是55,明天它可能是其他东西..有没有其他选项来获得入门价值?例如,我在该表单上只有一个字段,其类型为" total",我需要它的值,所以我想做类似的事情:

$entry['type'] == 'numer'

但我无论如何都可以在不使用绝对数字的情况下获得输入数据。 有人知道更灵活的方式吗?

1 个答案:

答案 0 :(得分:2)

$ entry将包含当前表单提交的所有字段ID和值的关联数组。因此,如果您的总计字段的ID为20,您可以访问当前提交的总金额,如下所示:$ entry ['20']。字段的ID不会更改,因此$ entry ['20']将始终返回总计。更多详情: https://www.gravityhelp.com/documentation/article/entry-object/

如果您有许多不同的表单,并且每个表单的字段ID不同,那么您可以使用:

$fields = GAPI::get_fields_by_type( $form, $type );

因此,如果您知道您只有一个数字字段,则可以获得如下ID:

$number_fields = GAPI::get_fields_by_type( $form, 'number' );
$my_number_field = $number_fields[0];
$field_id = $my_number_field->id;