我有一个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行代码,但我卡住了。
1条答案
按热度按时间2ekbmq321#
下面是一个类做以下事情:
1.在
wpcf7_before_send_mail
中获取一个动态重定向URL(您可以在那里添加API调用)1.将该重定向URL添加到
wpcf7_feedback_response
中的CF7响应对象1.将CF7 DOM事件的JavaScript侦听器注入
wp_footer
的前端1.如果已设置重定向目标,则在DOM事件
wpcf7mailsent
上重定向用户字符串
希望这对你有帮助。