在重力表单输入上创建自定义数组键

时间:2020-04-18 作者:joshmoto

当我提交重力表条目时$entry 始终将我的输入键作为字段ID返回。

问题是,我有多个表单使用gform_after_submission 行动

因为重力表单使用字段ID作为输入键,这意味着我需要单独的函数或数组来处理每个表单以完成相同的操作。因为我的字段ID在每个表单上都是不同的。

是否有办法将重力表单输入字段键更改为自定义id字符串,而不是整数和浮点?

<pre>Array
(
    [id] => 63
    [status] => active
    [form_id] => 1
    [ip] => 172.25.0.1
    [source_url] => http://localhost/cart/
    [currency] => GBP
    [post_id] => 
    [date_created] => 2020-04-18 11:27:16
    [date_updated] => 2020-04-18 11:27:16
    [is_starred] => 0
    [is_read] => 0
    [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36
    [payment_status] => 
    [payment_date] => 
    [payment_amount] => 
    [payment_method] => 
    [transaction_id] => 
    [is_fulfilled] => 
    [created_by] => 1
    [transaction_type] => 
    [1.1] => 
    [3] => collection
    [24] => 
    [13] => Josh
    [14] => Moto
    [15] => [email protected]
    [16] => 01234567890
    [17] => 09876543210
    [18] => 09876543210
    [20.1] => pay-by-phone
    [21.1] => confirm-submission
    [22.1] => accept-terms
)
</pre>
这只是我所做工作的扩展。请参见下面的我的代码。

add_action(\'gform_after_submission_1\', [ $this, \'create_order\' ], 10, 2 );

public function create_order( $entry, $form ) {

    // get the current cart data array
    $data = self::data();

    // create an order array
    $order = [
        \'post_author\'   => $user_id,
        \'post_content\'  => json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
        \'post_type\'     => \'purchase-order\',
        \'post_status\'   => \'publish\'
    ];

    // create order post using an array and return the post id
    $result = wp_insert_post($order);

    // if post id and is not a wp error then
    if($result && !is_wp_error($result)) {

        // get the id
        $post_id = $result;

        // update personal details
        update_field(\'order_first_name\',$entry[\'13\'],$post_id);

        // more update field functions...


    }

}
所以,正如你们所看到的,我想做的是update_field 作用

例如,我使用字段id的输入值$entry[\'13\'].

但我希望字段id是字符串,这样我就可以使用update_field 像这样。。。

update_field(\'order_first_name\',$entry[\'order_first_name\'],$post_id);
返回的$entry 数组应该是这样的。。。

Array
(
    [id] => 63
    [status] => active
    [form_id] => 1
    [ip] => 172.25.0.1
    [source_url] => http://localhost/cart/
    [currency] => GBP
    [post_id] => 
    [date_created] => 2020-04-18 11:27:16
    [date_updated] => 2020-04-18 11:27:16
    [is_starred] => 0
    [is_read] => 0
    [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36
    [payment_status] => 
    [payment_date] => 
    [payment_amount] => 
    [payment_method] => 
    [transaction_id] => 
    [is_fulfilled] => 
    [created_by] => 1
    [transaction_type] => 
    [order_install.1] => 
    [order_delivery_type] => collection
    [order_username] => 
    [order_first_name] => Josh
    [order_last_name] => Moto
    [order_email] => [email protected]
    [order_telephone] => 01234567890
    [order_telephone_2] => 09876543210
    [order_whatsapp] => 09876543210
    [order_terms_payment.1] => pay-by-phone
    [order_terms_confirm.1] => confirm-submission
    [order_terms_accept.1] => accept-terms
)
<人力资源>@DaveWiz 谢谢你的回答,看看我是怎么用的。

/**
 * C
 * @param array $entry
 * @param array $form
 * @return void
 */
public static function key_conversion($entry, $form) {

    // admin value keys
    $order_first_name = \'order_first_name\';

    // for each fields
    foreach( $form[\'fields\'] as $field ) {

        // check case on admin label
        switch( $field->adminLabel ) {

            // order first name
            case $order_first_name:
                $entry[$order_first_name] = $entry[ $field->id ];
                unset($entry[ $field->id ]);
                break;

        }
    }

}

1 个回复
最合适的回答,由SO网友:Dave from Gravity Wiz 整理而成

我假设您的潜在需求是为不同表单上的每个字段分配一个角色。因此,表单A上的名称字段可能是字段ID 1,而表单B上的名称字段可能是字段ID 2。这两个字段都表示“主要联系人姓名”的角色。实现这一点的最简单方法是使用Admin Label字段设置为字段指定此“角色”。然后,您可以获取给定条目的表单对象,遍历字段,检查所需的管理标签(例如角色),并对数据执行所需的操作。

$form = GFAPI::get_form( $entry[\'form_id\'] );

foreach( $form[\'fields\'] as $field ) {
    switch( $field->adminLabel ) {
        case \'primary_contact_name\':
            $primary_contact_name = $entry[ $field->id ];
            break;
    }
}
这只是一个粗略的例子,说明了它是如何作为一种可靠的方式从多个条目中获取所需的特定类型的数据的。

相关推荐