是否以重力形式将数据从After_Submit操作传递到确认筛选器?

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

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

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

    }

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

我需要通过我的$post_id 到重力窗体gform_confirmation 滤器但我真的很难看到如何做到这一点。

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;

}
如果有人有任何想法,那就太好了,谢谢。

1 个回复
SO网友:joshmoto

针对这种情况的一种黑客方法就是获取最新的create帖子。这并不理想,但只要没有人在几毫秒内创建订单,就可以正常工作。

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;

}

相关推荐