From 3d891c588cc56b0fe1913468108b9e39bdf366ed Mon Sep 17 00:00:00 2001 From: maksim Date: Fri, 4 Jul 2025 11:18:26 +0300 Subject: [PATCH] Andrei | b24 integration fixex --- .../modules/forms/module-ajax-controller.php | 88 ++++++++++++- .../modules/forms/module-controller.php | 118 ++++++++++-------- .../cosmopet/templates/forms/author-form.twig | 4 +- .../cosmopet/templates/shop/cart-bottom.twig | 2 +- .../woocommerce/checkout/form-checkout.php | 2 +- 5 files changed, 156 insertions(+), 58 deletions(-) diff --git a/wp-content/themes/cosmopet/modules/forms/module-ajax-controller.php b/wp-content/themes/cosmopet/modules/forms/module-ajax-controller.php index 7c94dff..bfed83b 100644 --- a/wp-content/themes/cosmopet/modules/forms/module-ajax-controller.php +++ b/wp-content/themes/cosmopet/modules/forms/module-ajax-controller.php @@ -18,6 +18,51 @@ function get_current_user_data(){ ); } +add_action('wp_ajax_blogauth_form', function() { + $enabledHandlers = ['b24', 'email', 'tg']; + $user_data = get_current_user_data(); + $formData = json_decode($_POST['formData']); + $crmData = array( + 'phone' => $formData['phone'], + 'name' => $formData['name'], + 'email' => $formData['email'], + 'msg' => $formData['msg'], + 'url' => $formData['url'], + 'stage' => 'C4:NEW', + 'fName' => 4, + 'order_total' => 0, + 'user_id' => $user_data['user_id'], + 'method' => 'crm.deal.add', + 'form_title' => 'Предложить статью или стать автором', + 'is_subscribe' => false, + 'order_id' => '', + ); + process_form($enabledHandlers, $crmData); +}); + +add_action('wp_ajax_nopriv_blogauth_form', function() { + $enabledHandlers = ['b24', 'email', 'tg']; + $user_data = get_current_user_data(); + $formData = json_decode($_POST['formData']); + $crmData = array( + 'phone' => null, + 'name' => $formData['name'], + 'email' => $formData['email'], + 'msg' => $formData['msg'], + 'url' => $formData['url'], + 'stage' => 'C4:NEW', + 'fName' => 4, + 'order_total' => 0, + 'user_id' => $user_data['user_id'], + 'method' => 'crm.deal.add', + 'form_title' => 'Предложить статью или стать автором', + 'is_subscribe' => false, + 'order_id' => '', + ); + process_form($enabledHandlers, $crmData); +}); + + add_action('wp_ajax_contact_form', function() { $enabledHandlers = ['b24', 'email', 'tg']; $user_data = get_current_user_data(); @@ -162,8 +207,6 @@ function process_form($enabledHandlers, $crmData = array()) { wp_send_json($response); } - - // Отправляем данные в Bitrix24 при создании заказа add_action('woocommerce_checkout_order_created', 'send_order_to_bitrix_on_create', 10, 1); @@ -250,17 +293,50 @@ function send_order_data($order_id) { // Добавляем товары (префикс PRODUCT_1_, PRODUCT_2_ и т.д.) $product_index = 1; + $product_summary = "🛒 Состав заказа:\n\n"; 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_summary .= "{$product_index}) {$name}\n"; + $product_summary .= " Кол-во: {$qty}\n"; + $product_summary .= " Сумма: {$total}\n"; + if ($sku) { + $product_summary .= " Артикул: {$sku}\n"; + } + $product_summary .= "\n"; + $product_index++; } - $handler = FormHandlerFactory::getHandler(['tg']); - $response = $handler->handle($formData, array()); + $message = "📋 Данные формы:\n\n"; + foreach ($formData as $key => $value) { + if (is_array($value)) { + $value = implode(', ', $value); // на случай, если в значении массив + } + $message .= sprintf("%s: %s\n", $key, $value); + } + $crmData = array( + 'phone' => $customer_phone, + 'name' => $customer_name, + 'email' => $customer_email, + 'msg' => $product_summary, + 'url' => null, + 'stage' => 'C2:UC_ZMY1QV', + 'fName' => 2, + 'order_total' => $order_total, + 'user_id' => $user_data['user_id'], + 'method' => 'crm.deal.add', + 'form_title' => 'Заказ на сайте', + 'is_subscribe' => false, + 'order_id' => $order_id, + ); + $handler = FormHandlerFactory::getHandler(['tg', 'b24']); + $response = $handler->handle($formData, $crmData); } add_action('woocommerce_payment_complete', 'notify_order_paid', 10, 1); @@ -278,5 +354,9 @@ function notify_order_paid($order_id) { $handler = FormHandlerFactory::getHandler(['tg']); $handler->handle($formData, []); + + $b24Handler = new b24Handler(); + $b24Handler->b24_update_deal_stage_by_order_id($order_id); } + ?> \ No newline at end of file diff --git a/wp-content/themes/cosmopet/modules/forms/module-controller.php b/wp-content/themes/cosmopet/modules/forms/module-controller.php index a6a8426..cefee07 100644 --- a/wp-content/themes/cosmopet/modules/forms/module-controller.php +++ b/wp-content/themes/cosmopet/modules/forms/module-controller.php @@ -52,9 +52,6 @@ class b24Handler extends FormHandler { $crmData['is_subscribe'], $crmData['order_id'], ); - error_log('----------CRM_DATA----------------'); - error_log(json_encode($crmData)); - error_log('----------CRM_DATA----------------'); // Вызываем следующий обработчик в цепочке return parent::handle($data, $crmData); } @@ -78,7 +75,7 @@ class b24Handler extends FormHandler { $searchFilters = []; if (!empty($userID)) { - $searchFilters[] = ['UF_CRM_1744373080655' => $userID]; + $searchFilters[] = ['UF_CRM_1751610129971' => $userID]; } if (!empty($phone)) $searchFilters[] = ['PHONE' => $phone]; @@ -87,7 +84,7 @@ class b24Handler extends FormHandler { foreach ($searchFilters as $filter) { $response = $this->b24_request('crm.contact.list', [ 'filter' => $filter, - 'select' => ['ID', 'NAME', 'PHONE', 'EMAIL', 'UF_CRM_1744373080655', 'IM'] + 'select' => ['ID', 'NAME', 'PHONE', 'EMAIL', 'UF_CRM_1751610129971', 'IM'] ]); if (!empty($response['result'])) { @@ -103,7 +100,7 @@ class b24Handler extends FormHandler { foreach ($foundContacts as $contact) { $matches = 0; - if (!empty($userID) && isset($contact['UF_CRM_1744373080655']) && $contact['UF_CRM_1744373080655'] == $userID) { + if (!empty($userID) && isset($contact['UF_CRM_1751610129971']) && $contact['UF_CRM_1751610129971'] == $userID) { $matches += 3; } @@ -145,8 +142,8 @@ class b24Handler extends FormHandler { $updateFields['UTM_SOURCE'] = $utm; } - if (!empty($userID) && (!isset($bestMatch['UF_CRM_1744373080655']) || $bestMatch['UF_CRM_1744373080655'] != $userID)) { - $updateFields['UF_CRM_1744373080655'] = $userID; + if (!empty($userID) && (!isset($bestMatch['UF_CRM_1751610129971']) || $bestMatch['UF_CRM_1751610129971'] != $userID)) { + $updateFields['UF_CRM_1751610129971'] = $userID; } if ($isSubscribe) { @@ -197,7 +194,7 @@ class b24Handler extends FormHandler { $newContact['fields']['UTM_SOURCE'] = $utm_source; } if (!empty($userID)) { - $newContact['fields']['UF_CRM_1744373080655'] = $userID; + $newContact['fields']['UF_CRM_1751610129971'] = $userID; } if ($isSubscribe) { $newContact['fields']['UF_CRM_1744562461053'] = true; @@ -342,8 +339,8 @@ class b24Handler extends FormHandler { // Ищем контакт по ID пользователя $response = $this->b24_request('crm.contact.list', [ - 'filter' => ['UF_CRM_1744373080655' => $userID], - 'select' => ['ID', 'NAME', 'PHONE', 'EMAIL', 'UF_CRM_1744373080655', 'IM', 'UF_CRM_1744562461053'] + 'filter' => ['UF_CRM_1751610129971' => $userID], + 'select' => ['ID', 'NAME', 'PHONE', 'EMAIL', 'UF_CRM_1751610129971', 'IM', 'UF_CRM_1744562461053'] ]); if (empty($response['result'])) { @@ -412,6 +409,46 @@ class b24Handler extends FormHandler { return ['success' => true, 'contact_id' => $contactId]; } + + + function b24_update_deal_stage_by_order_id($order_id, $new_stage = 'C2:FINAL_INVOICE') { + if (empty($order_id)) { + return ['success' => false, 'error' => 'Не указан order_id']; + } + + $b24 = new b24Handler(); + + // 1. Найдём сделку по полю UF_CRM_1745741833259 + $searchResponse = $b24->b24_request('crm.deal.list', [ + 'filter' => ['UF_CRM_1745741833259' => $order_id], + 'select' => ['ID', 'TITLE', 'STAGE_ID'] + ]); + + if (!empty($searchResponse['error'])) { + return ['success' => false, 'error' => 'Ошибка поиска сделки: ' . $searchResponse['error']]; + } + + $deals = $searchResponse['result'] ?? []; + + if (empty($deals)) { + return ['success' => false, 'error' => 'Сделка с таким order_id не найдена']; + } + + $dealId = $deals[0]['ID']; + + // 2. Обновим стадию сделки + $updateResponse = $b24->b24_request('crm.deal.update', [ + 'id' => $dealId, + 'fields' => ['STAGE_ID' => $new_stage] + ]); + + if (!empty($updateResponse['error'])) { + return ['success' => false, 'error' => 'Ошибка обновления стадии: ' . $updateResponse['error']]; + } + + return ['success' => true, 'deal_id' => $dealId]; + } + } class zohoHandler extends FormHandler { @@ -518,48 +555,31 @@ class FormHandlerFactory { } +// обработка обновления профиля пользователя +add_action('profile_update', 'handle_profile_update', 10, 2); + function handle_profile_update($userID, $old_user_data) { + $user = get_user_by('ID', $userID); -// // Пример обработки обновления профиля пользователя -// add_action('profile_update', 'handle_profile_update', 10, 2); - -// function handle_profile_update($userID, $old_user_data) { -// $user = get_user_by('ID', $userID); - -// $email = $user->user_email; -// $phone = get_user_meta($userID, 'billing_phone', true); -// $first_name = get_user_meta($userID, 'first_name', true); -// $last_name = get_user_meta($userID, 'last_name', true); -// $name = trim($first_name . ' ' . $last_name); - -// $newData = [ -// 'name' => $name, -// 'email' => $email, -// 'phone' => $phone, -// ]; - -// $result = b24_update_contact_by_user_id($userID, $newData); - -// if (!$result['success']) { -// error_log('Ошибка обновления контакта в Bitrix24: ' . $result['error']); -// } -// } + $email = $user->user_email; + $phone = get_user_meta($userID, 'billing_phone', true); + $first_name = get_user_meta($userID, 'first_name', true); + $last_name = get_user_meta($userID, 'last_name', true); + $name = trim($first_name . ' ' . $last_name); + $newData = [ + 'name' => $name, + 'email' => $email, + 'phone' => $phone, + ]; + $handler = new b24Handler(); + $result = $handler->b24_update_contact_by_user_id($userID, $newData); -// add_action('woocommerce_thankyou', 'add_b24_tracking_script', 10, 1); + if (!$result['success']) { + error_log('Ошибка обновления контакта в Bitrix24: ' . $result['error']); + } + } -// function add_b24_tracking_script($order_id) { -// if (!$order_id) return; - -// $order = wc_get_order($order_id); - -// echo ''; -// } function get_utm_data() { $utm_data = []; @@ -572,7 +592,6 @@ function get_utm_data() { return $utm_data; } - // Функция для сохранения UTM в куки (если нужно хранить между сессиями) function save_utm_to_cookies() { $utm_params = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term']; @@ -583,5 +602,4 @@ function save_utm_to_cookies() { } } } - save_utm_to_cookies(); \ No newline at end of file diff --git a/wp-content/themes/cosmopet/templates/forms/author-form.twig b/wp-content/themes/cosmopet/templates/forms/author-form.twig index 2e9aa6c..3570af4 100644 --- a/wp-content/themes/cosmopet/templates/forms/author-form.twig +++ b/wp-content/themes/cosmopet/templates/forms/author-form.twig @@ -5,7 +5,7 @@

ПРЕДЛОЖИТЕ СТАТЬЮ

или станьте автором

-
+
@@ -18,7 +18,7 @@
- +
diff --git a/wp-content/themes/cosmopet/templates/shop/cart-bottom.twig b/wp-content/themes/cosmopet/templates/shop/cart-bottom.twig index 9d70685..1cdfba0 100644 --- a/wp-content/themes/cosmopet/templates/shop/cart-bottom.twig +++ b/wp-content/themes/cosmopet/templates/shop/cart-bottom.twig @@ -2,7 +2,7 @@ diff --git a/wp-content/themes/cosmopet/woocommerce/checkout/form-checkout.php b/wp-content/themes/cosmopet/woocommerce/checkout/form-checkout.php index 7a433b1..8cd1958 100644 --- a/wp-content/themes/cosmopet/woocommerce/checkout/form-checkout.php +++ b/wp-content/themes/cosmopet/woocommerce/checkout/form-checkout.php @@ -10,5 +10,5 @@ if (!defined('ABSPATH')) { exit; } - +$context = Timber::context(); Timber::render('shop/checkout.twig', $context); \ No newline at end of file