最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Creating custom array keys on gravity forms entry

programmeradmin5浏览0评论

When I submit my gravity form entry, the $entry always returns my entry keys as the field ID's.

The problem with this is that I have multiple forms that perform similar functions using the gform_after_submission action.

Because gravity forms uses the field ID's as entry keys, it means I need separate functions or arrays to handle each form to do the same thing. Because my field ID's on each form are different.

Is there way change the gravity form entry field keys to a custom id string rather than integer and floats?

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

Just an expansion of what i'm doing. See my code below.

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...


    }

}

So what I'm trying to do as you can see with the update_field function.

Is that I'm using entry values by field id, for example $entry['13'].

But I would rather the field id's be strings, so i can use update_field like this...

update_field('order_first_name',$entry['order_first_name'],$post_id);

The returned $entry array would some how look like this...

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 Thanks for your answer, see how I used it.

/**
 * 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;

        }
    }

}

When I submit my gravity form entry, the $entry always returns my entry keys as the field ID's.

The problem with this is that I have multiple forms that perform similar functions using the gform_after_submission action.

Because gravity forms uses the field ID's as entry keys, it means I need separate functions or arrays to handle each form to do the same thing. Because my field ID's on each form are different.

Is there way change the gravity form entry field keys to a custom id string rather than integer and floats?

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

Just an expansion of what i'm doing. See my code below.

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...


    }

}

So what I'm trying to do as you can see with the update_field function.

Is that I'm using entry values by field id, for example $entry['13'].

But I would rather the field id's be strings, so i can use update_field like this...

update_field('order_first_name',$entry['order_first_name'],$post_id);

The returned $entry array would some how look like this...

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 Thanks for your answer, see how I used it.

/**
 * 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;

        }
    }

}
Share Improve this question edited Apr 19, 2020 at 18:30 joshmoto asked Apr 18, 2020 at 11:55 joshmotojoshmoto 4676 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

I'm assuming your underlying need is to assign a role for each field on different forms. So the Name field on Form A might be field ID 1 and the Name field on Form B might be field ID 2. Both fields represent the role of "Primary Contact Name". The simplest way to do this would be to give the field this "role" with the Admin Label field setting. Then you can fetch for the form object for the given entry, loop through the fields, check for your desired Admin Label (e.g. role) and do what you need to do with the data.

$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;
    }
}

That's just a rough example of how it might work as a reliable way to get the specific type of data you want from multiple entries.

发布评论

评论列表(0)

  1. 暂无评论