首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据从after_submission动作传递给重力形式的确认过滤器?

将数据从after_submission动作传递给重力形式的确认过滤器?
EN

WordPress Development用户
提问于 2020-04-20 13:21:14
回答 2查看 924关注 0票数 0

我正在gform_after_submission操作中创建一个post,它在成功创建post时设置一个post ID变量。

https://docs.gravityforms.com/gform_之后_提交/

代码语言:javascript
复制
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...

    }

}

因为我的表单是通过ajax提交的,所以不能调用上面的header php重定向,因为重定向只会在ajax请求中发生。

我需要以某种方式将我的$post_id传递给重力形式的gform_confirmation过滤器。但我真的很想知道该怎么做。

https://docs.gravityforms.com/gform_确认/确认

代码语言:javascript
复制
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;

}

如果有人有什么想法,那就太好了,谢谢。

EN

回答 2

WordPress Development用户

发布于 2020-04-20 13:57:19

针对这种情况,一个麻烦的方法就是获得最新的create帖子。不是理想的,但只要没有人在彼此毫秒内创造秩序就行了。

代码语言:javascript
复制
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
EN

WordPress Development用户

发布于 2022-05-23 13:44:11

考虑到gform_confirmation还允许提交后访问$confirmation、$form和$entry参数,您可以使用它来代替gform_after_submission

参考文献:https://docs.gravityforms.com/gform_确认/确认

票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/364595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档