You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
cosmopet-architecture/wp-content/themes/cosmopet/modules/forms/module-ajax-controller.php

145 lines
4.8 KiB

<?php
require_once 'module-controller.php';
add_action('wp_ajax_contact_form', function() {
$enabledHandlers = ['b24', 'email', 'tg'];
process_form($enabledHandlers);
});
add_action('wp_ajax_nopriv_contact_form', function() {
$enabledHandlers = ['b24', 'email', 'tg'];
process_form($enabledHandlers);
});
add_action('wp_ajax_subscribe_form', function() {
$enabledHandlers = ['b24', 'email', 'tg'];
process_form($enabledHandlers);
});
add_action('wp_ajax_nopriv_subscribe_form', function() {
$enabledHandlers = ['b24', 'email', 'tg'];
process_form($enabledHandlers);
});
add_action('wp_ajax_to_know_form', function() {
$enabledHandlers = ['b24', 'tg'];
process_form($enabledHandlers);
});
add_action('wp_ajax_nopriv_to_know_form', function() {
$enabledHandlers = ['b24', 'tg'];
process_form($enabledHandlers);
});
function process_form($enabledHandlers) {
$formData = $_POST['formData'];
$handler = FormHandlerFactory::getHandler($enabledHandlers);
$response = $handler->handle($formData);
wp_send_json($response);
}
add_action('woocommerce_thankyou', 'send_order_data_on_payment', 10, 1);
function send_order_data_on_payment($order_id) {
$order = wc_get_order($order_id);
$already_sent = $order->get_meta('_order_sent');
if (!$already_sent) {
// Отправляем заказ в Bitrix24
send_order_data($order_id);
// Помечаем заказ как отправленный, чтобы избежать дублирования
$order->update_meta_data('_order_sent', '1');
$order->save();
}
}
function send_order_data($order_id) {
$order = wc_get_order($order_id);
// 1. Получаем данные заказа
$customer_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
$customer_phone = $order->get_billing_phone();
$customer_email = $order->get_billing_email();
$order_total = $order->get_total();
$order_comment = '';
$utm = get_utm_data()['utm_source'];
$url = '';
$fName = 2;
// 2. Формируем список товаров
foreach ($order->get_items() as $item) {
$product = $item->get_product();
$order_comment .= sprintf(
"%s x %s = %s\n",
$product->get_name(),
$item->get_quantity(),
wc_price($item->get_total())
);
}
$order_comment .= "\n=== ДАННЫЕ ДОСТАВКИ ===\n";
$order_comment .= "Город: " . $order->get_billing_city() . "\n";
$order_comment .= "Адрес: " . $order->get_billing_address_1() . "\n";
if ($order->get_billing_address_2()) {
$order_comment .= "Доп. адрес: " . $order->get_billing_address_2() . "\n";
}
$order_comment .= "Способ доставки: " . $order->get_shipping_method() . "\n";
$order_comment .= "Комментарий к заказу: " . $order->get_customer_note() . "\n";
if (is_user_logged_in()) {
$user_id = get_current_user_id();
}
else{
$user_id = '';
}
$form_title = 'Покупка на сайте';
$formData = [
// Основные данные
'TITLE' => 'Заказ #' . $order_id,
'NAME' => $customer_name,
'PHONE' => $customer_phone,
'EMAIL' => $customer_email,
'USER_ID' => $user_id,
'ORDER_ID' => $order_id,
'ORDER_TOTAL' => $order_total,
'PAYMENT_METHOD' => $order->get_payment_method_title(),
'COMMENTS' => $order_comment,
// Адрес (префикс ADDR_)
'ADDR_CITY' => $order->get_billing_city(),
'ADDR_STREET' => $order->get_billing_address_1(),
'ADDR_STREET2' => $order->get_billing_address_2(),
// UTM-метки (префикс UTM_)
'UTM_SOURCE' => $utm,
'UTM_MEDIUM' => get_utm_data()['utm_medium'] ?? '',
'UTM_CAMPAIGN' => get_utm_data()['utm_campaign'] ?? '',
// Системные поля
'SOURCE' => 'WooCommerce',
'FNAME' => $fName,
];
// Добавляем товары (префикс PRODUCT_1_, PRODUCT_2_ и т.д.)
$product_index = 1;
foreach ($order->get_items() as $item) {
$product = $item->get_product();
$formData["PRODUCT_{$product_index}_NAME"] = $product->get_name();
$formData["PRODUCT_{$product_index}_QTY"] = $item->get_quantity();
$formData["PRODUCT_{$product_index}_PRICE"] = $item->get_total();
$formData["PRODUCT_{$product_index}_SKU"] = $product->get_sku();
$product_index++;
}
$handler = FormHandlerFactory::getHandler(['tg']);
$response = $handler->handle($formData);
}
?>