0

I am creating a post within gform_after_submission action, which sets a post ID variable when the post is successfully created.

https://docs.gravityforms.com/gform_after_submission/

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

    // user id
    $user_id = get_current_user_id();

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

        // my order custom field updates go here...

    }

}


Because my form is submitting via ajax, I can't invoke a header php redirect above because the redirect will just happen within the ajax request.

I need to somehow pass my $post_id to the gravity forms gform_confirmation filter. But I'm really struggling to see how this can be done.

https://docs.gravityforms.com/gform_confirmation/

add_filter('gform_confirmation_1', [ $this, 'order_confirmation' ], 10, 4 );

public function order_confirmation( $confirmation, $form, $entry, $ajax ) {

    // update redirect to order
    $confirmation = array( 'redirect' => get_permalink($post_id) );

    // return confirmation
    return $confirmation;

}


If anyone has any ideas that would be great thanks.

2 Answers 2

1

A hacky way round just for this situation is to just to get the latest create post. Not ideal but works as long no one creates an order within milliseconds of each other.

public function order_confirmation( $confirmation, $form, $entry, $ajax ) {

    // get latest created order
    $order = get_posts([
        'post_type' => 'purchase-order',
        'numberposts' => 1
    ]);

    // update redirect to order
    $confirmation = array( 'redirect' => get_permalink($order[0]->ID) );

    // return confirmation
    return $confirmation;

}
1

Given that the gform_confirmation also allows post-submission access to the $confirmation, $form and $entry parameters you could just use that in place of gform_after_submission

Ref: https://docs.gravityforms.com/gform_confirmation/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.