wordpress 在邮件发送钩子响应之前,将用户发送到Contactform7中的动态URL

jhdbpxl9  于 2023-11-17  发布在  WordPress
关注(0)|答案(1)|浏览(140)

我有一个cf 7和一个钩子在我的function.php文件如下所示。我想发送一个用户到动态URL,我将在该钩子接收。

add_action('wpcf7_before_send_mail', 'send_order_data_to_api', 10, 3);

function send_order_data_to_api($contact_form, $abort, $submission)
{
$wp_form = WPCF7_ContactForm::get_current();
$order_id = $submission->get_posted_data('order_id');

if ($wp_form->id() == 860 && !empty($order_id)) {
    $admin_url = $submission->get_posted_data('admin_url');
    $admin_username = $submission->get_posted_data('admin_username');
    $admin_pass = $submission->get_posted_data('admin_pass');
    $email = $submission->get_posted_data('email');
    $note = $submission->get_posted_data('note');
    $order = wc_get_order($order_id);
    //$url = "https://qa-dashboard.wordpressfaster.com/api/v1/signup-user";
    $url = "https://api.npoint.io/a2275b7debf2832643bb";
    $product = $product_id = $product_name = $qty = "";

    foreach ($order->get_items() as $item_id => $item) {
        $product = $item->get_product();
        $product_id = $product->get_id();
        $product_name = $item->get_name();
        $qty = $item->get_quantity();
    }

    $order_data = $order->get_data(); 

    $response = wp_remote_post($url, array(
        'headers'     => array(
            //'Authorization' => "Token my_token",
            'Content-Type'  => 'application/json; charset=utf-8',
        ),
        'body' => json_encode(array(
            "firstName" => $order->get_billing_first_name(),
            "lastName" => $order->get_billing_last_name(),
            "email" => $order->get_billing_email(),
            "company" => $order->get_billing_company(),
            "address1" => $order->get_billing_address_1(),
            "address2" => $order->get_billing_address_2(),
            "city" => $order->get_billing_city(),
            "state" => $order->get_billing_state(),
            "postcode" => $order->get_billing_postcode(),
            "country" => $order->get_billing_country(),
            "phone" => $order->get_billing_phone(),
            "transactionId" => $order->get_transaction_id(),
            "orderId" => $order->get_id(),
            "currency" => $order->get_currency(),
            "total" => $order->get_total(),
            "productId" => '43',
            "planName" => $product_name,
            "quantity" => $qty,
            "contactEmail" =>  $email,
            "contactFirstName" => $order->get_billing_first_name(),
            "contactLastName" => $order->get_billing_last_name(),
            "contactPassword" => $admin_pass,
            "contactCompany" => $order->get_billing_company(),
            "contactAddress" => $order->get_billing_address_1(),
            "contactCity" => $order->get_billing_city(),
            "contactState" => $order->get_billing_state(),
            "contactPostcode" => $order->get_billing_postcode(),
            "contactCountry" => $order->get_billing_country(),
            "contactPhone" => $order->get_billing_phone(),
            "adminUrl" => $admin_url,
            "adminUsername" => $admin_username,
        )),
        'method'      => 'POST',
        'data_format' => 'body',
    ));

    if (is_wp_error($response)) {
        $error_message = $response->get_error_message();
        throw new Exception($error_message);
    }
}
}

字符串
上面是我的代码。我想在用户填写表单时将其重定向到我在$response变量中的JSON数据中接收的URL。
我已经尝试了几种不同的方法,仍然没有成功。我需要有人来帮助我在这种情况下。它可能是2-5行代码,但我卡住了。

2ekbmq32

2ekbmq321#

下面是一个类做以下事情:
1.在wpcf7_before_send_mail中获取一个动态重定向URL(您可以在那里添加API调用)
1.将该重定向URL添加到wpcf7_feedback_response中的CF7响应对象
1.将CF7 DOM事件的JavaScript侦听器注入wp_footer的前端
1.如果已设置重定向目标,则在DOM事件wpcf7mailsent上重定向用户

class custom_wpcf7_helper {
    protected static $redirect_target = null;

    public static function wpcf7_before_send_mail($contact_form, $abort, $submission) {
        // DO YOUR API REQUEST HERE:
        $url = 'https://google.com/?q=' . rand(1000, 99999999); // For demonstration

        // THEN SET REDIRECT URL:
        self::$redirect_target = $url;

        
    }

    public static function wpcf7_feedback_response($response, $result) {
        if(!empty(self::$redirect_target)) {
            $response['redirect'] = self::$redirect_target;
        }
        return $response;
    }

    public static function wp_footer() {
        echo '
            <script>
                document.addEventListener(\'wpcf7mailsent\', function( event ) {
                    try {
                        console.log(\'Redirecting to \' + event.detail.apiResponse.redirect);
                        if(typeof(event.detail.apiResponse.redirect) != \'undefined\') {
                            window.location = event.detail.apiResponse.redirect;
                        }
                    } catch(e) {
                        console.log(e);
                    }
                }, false );
            </script>
        ';
    }
}
add_action('wpcf7_before_send_mail', ['custom_wpcf7_helper', 'wpcf7_before_send_mail'], 10, 3);
add_action('wp_footer', ['custom_wpcf7_helper', 'wp_footer']);
add_filter('wpcf7_feedback_response', ['custom_wpcf7_helper', 'wpcf7_feedback_response'], 10, 2);

字符串
希望这对你有帮助。

相关问题