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

问题描述 投票:0回答:1

我的 function.php 文件中有一个 cf7 和一个钩子,如下所示。我想将用户发送到我将在该挂钩中收到的动态 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 行代码,但我被困住了。

php wordpress contact-form-7
1个回答
0
投票

这是一个执行以下操作的类:

  1. wpcf7_before_send_mail
    中获取动态重定向 URL(您可以在那里添加 API 调用)

  2. 将该重定向 URL 添加到

    wpcf7_feedback_response

    中的 CF7 响应对象
  3. 将 CF7 DOM 事件的 JavaScript 监听器注入到前端

    wp_footer

  4. 在 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);

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.