From de3195c897a0dd71e593b7bcc573ed995c1bad2d Mon Sep 17 00:00:00 2001 From: maksim Date: Thu, 26 Jun 2025 15:12:21 +0300 Subject: [PATCH] =?UTF-8?q?Task=206989=20|=20=D0=B8=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D1=83?= =?UTF-8?q?=20=D0=BE=D1=82=D0=BA=D1=80=D1=8B=D1=82=D0=B8=D1=8F=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B4=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D1=85=20=D0=BE=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=20(=D0=BD=D0=B5=20=D0=BE=D1=82=D0=BA=D1=80=D1=8B?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B8=D1=81=D1=8C=20=D0=B2=20ajax=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B3=D1=80=D1=83=D0=B6=D0=B5=D0=BD=D0=BD=D1=8B=D1=85=20?= =?UTF-8?q?=D0=BA=D0=B0=D1=80=D1=82=D0=BE=D1=87=D0=BA=D0=B0=D1=85),=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=20=D0=B8=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=B8=D0=BB=20=D0=BB=D0=BE=D0=B3?= =?UTF-8?q?=D0=B8=D0=BA=D1=83=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=B2=20b24?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/forms/module-ajax-controller.php | 140 ++++++++++++++++-- .../modules/forms/module-controller.php | 74 +++++---- .../cosmopet/modules/popup/assets/js/modal.js | 16 +- .../cosmopet/templates/modal/to_know.twig | 2 +- 4 files changed, 184 insertions(+), 48 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 cd835ab..414963f 100644 --- a/wp-content/themes/cosmopet/modules/forms/module-ajax-controller.php +++ b/wp-content/themes/cosmopet/modules/forms/module-ajax-controller.php @@ -2,42 +2,162 @@ require_once 'module-controller.php'; +function get_current_user_data(){ + if (is_user_logged_in()) { + $user_id = get_current_user_id(); + $tg_username = get_user_meta($user_id, 'tg_username', true); + } + else{ + $user_id = ''; + $tg_username = ''; + } + return array( + 'user_id' => $user_id, + 'tg_id' => $tg_username, + ); +} + add_action('wp_ajax_contact_form', function() { $enabledHandlers = ['b24', 'email', 'tg']; - process_form($enabledHandlers); + $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' => 'C7:NEW', + 'fName' => 0, + 'order_total' => 0, + 'user_id' => $user_data['user_id'], + 'method' => 'crm.lead.add', + 'form_title' => 'Форма в футере', + 'is_subscribe' => false, + 'order_id' => '', + ); + process_form($enabledHandlers, $crmData); }); add_action('wp_ajax_nopriv_contact_form', function() { $enabledHandlers = ['b24', 'email', 'tg']; - process_form($enabledHandlers); + $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' => 'C7:NEW', + 'fName' => 0, + 'order_total' => 0, + 'user_id' => $user_data['user_id'], + 'method' => 'crm.lead.add', + 'form_title' => 'Форма в футере', + 'is_subscribe' => false, + 'order_id' => '', + ); + process_form($enabledHandlers, $crmData); }); add_action('wp_ajax_subscribe_form', function() { $enabledHandlers = ['b24', 'email', 'tg']; - process_form($enabledHandlers); + $user_data = get_current_user_data(); + $formData = json_decode($_POST['formData']); + $crmData = array( + 'phone' => null, + 'name' => null, + 'email' => $formData['email'], + 'msg' => null, + 'url' => $formData['url'], + 'stage' => 'C2:NEW', + 'fName' => 2, + 'order_total' => 0, + 'user_id' => $user_data['user_id'], + 'method' => 'crm.deal.add', + 'form_title' => 'Подписка на предложения', + 'is_subscribe' => true, + 'order_id' => '', + ); + process_form($enabledHandlers, $crmData); }); add_action('wp_ajax_nopriv_subscribe_form', function() { - $enabledHandlers = ['b24', 'email', 'tg']; - process_form($enabledHandlers); + $enabledHandlers = ['b24', 'email', 'tg']; + $user_data = get_current_user_data(); + $formData = json_decode($_POST['formData']); + $crmData = array( + 'phone' => null, + 'name' => null, + 'email' => $formData['email'], + 'msg' => null, + 'url' => $formData['url'], + 'stage' => 'C2:NEW', + 'fName' => 2, + 'order_total' => 0, + 'user_id' => $user_data['user_id'], + 'method' => 'crm.deal.add', + 'form_title' => 'Подписка на предложения', + 'is_subscribe' => true, + 'order_id' => '', + ); + process_form($enabledHandlers, $crmData); }); add_action('wp_ajax_to_know_form', function() { - $enabledHandlers = ['b24', 'tg']; - process_form($enabledHandlers); + $enabledHandlers = ['b24', 'tg']; + $user_data = get_current_user_data(); + $raw = "sub_product=...&email=...&url=..."; + parse_str($_POST['formData'], $formData); + $crmData = array( + 'phone' => null, + 'name' => null, + 'email' => $formData['email'], + 'msg' => 'Пользователь заинтересовался отсутсвующим продуктом' . $formData['sub_product'], + 'url' => $formData['url'], + 'stage' => 'C2:UC_QJ1M9Z', + 'fName' => 2, + '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_to_know_form', function() { $enabledHandlers = ['b24', 'tg']; - process_form($enabledHandlers); + $user_data = get_current_user_data(); + $raw = "sub_product=...&email=...&url=..."; + parse_str($_POST['formData'], $formData); + $crmData = array( + 'phone' => null, + 'name' => null, + 'email' => $formData['email'], + 'msg' => 'Пользователь заинтересовался отсутсвующим продуктом' . $formData['sub_product'], + 'url' => $formData['url'], + 'stage' => 'C2:UC_QJ1M9Z', + 'fName' => 2, + 'order_total' => 0, + 'user_id' => $user_data['user_id'], + 'method' => 'crm.deal.add', + 'form_title' => 'Подписка на отсутствующий продукт', + 'is_subscribe' => false, + 'order_id' => '', + ); + process_form($enabledHandlers, $crmData); + process_form($enabledHandlers, $crmData); }); -function process_form($enabledHandlers) { +function process_form($enabledHandlers, $crmData = array()) { $formData = $_POST['formData']; $handler = FormHandlerFactory::getHandler($enabledHandlers); - $response = $handler->handle($formData); + $response = $handler->handle($formData, $crmData); wp_send_json($response); } diff --git a/wp-content/themes/cosmopet/modules/forms/module-controller.php b/wp-content/themes/cosmopet/modules/forms/module-controller.php index 5fe6006..a6a8426 100644 --- a/wp-content/themes/cosmopet/modules/forms/module-controller.php +++ b/wp-content/themes/cosmopet/modules/forms/module-controller.php @@ -7,9 +7,9 @@ abstract class FormHandler { $this->nextHandler = $nextHandler; } - public function handle($data) { + public function handle($data, $crmData) { if ($this->nextHandler) { - return $this->nextHandler->handle($data); + return $this->nextHandler->handle($data, $crmData); } return ['success' => true, 'message' => 'Форма обработана']; } @@ -36,15 +36,30 @@ $utm_data = get_utm_data(); class b24Handler extends FormHandler { - public function handle($data) { - // Логика отправки в Mailchimp - error_log("Отправка в b24: " . json_encode($data)); - + public function handle($data, $crmData) { + $this->b24_add_lead( + $crmData['phone'], + $crmData['name'], + $crmData['email'], + $crmData['msg'], + $crmData['url'], + $crmData['stage'], + $crmData['fName'], + $crmData['order_total'], + $crmData['user_id'], + $crmData['method'], + $crmData['form_title'], + $crmData['is_subscribe'], + $crmData['order_id'], + ); + error_log('----------CRM_DATA----------------'); + error_log(json_encode($crmData)); + error_log('----------CRM_DATA----------------'); // Вызываем следующий обработчик в цепочке - return parent::handle($data); + return parent::handle($data, $crmData); } - function b24_get_or_create_contact($phone, $name, $email, $utm, $userID = null, $isSubscribe) { + function b24_get_or_create_contact($phone, $name, $email, $userID = null, $isSubscribe) { $contactId = null; $error = null; $foundContacts = []; @@ -70,7 +85,7 @@ class b24Handler extends FormHandler { if (!empty($email)) $searchFilters[] = ['EMAIL' => $email]; foreach ($searchFilters as $filter) { - $response = b24_request('crm.contact.list', [ + $response = $this->b24_request('crm.contact.list', [ 'filter' => $filter, 'select' => ['ID', 'NAME', 'PHONE', 'EMAIL', 'UF_CRM_1744373080655', 'IM'] ]); @@ -92,8 +107,8 @@ class b24Handler extends FormHandler { $matches += 3; } - if (!empty($phone) && valueExists($contact['PHONE'] ?? [], $phone)) $matches++; - if (!empty($email) && valueExists($contact['EMAIL'] ?? [], $email)) $matches++; + if (!empty($phone) && $this->valueExists($contact['PHONE'] ?? [], $phone)) $matches++; + if (!empty($email) && $this->valueExists($contact['EMAIL'] ?? [], $email)) $matches++; if ($matches > $maxMatches) { $maxMatches = $matches; @@ -109,11 +124,11 @@ class b24Handler extends FormHandler { $updateFields['NAME'] = $name; } - if (!empty($phone) && !valueExists($bestMatch['PHONE'] ?? [], $phone)) { + if (!empty($phone) && !$this->valueExists($bestMatch['PHONE'] ?? [], $phone)) { $updateFields['PHONE'][] = ['VALUE' => $phone, 'VALUE_TYPE' => 'MOBILE']; } - if (!empty($email) && !valueExists($bestMatch['EMAIL'] ?? [], $email)) { + if (!empty($email) && !$this->valueExists($bestMatch['EMAIL'] ?? [], $email)) { $updateFields['EMAIL'][] = ['VALUE' => $email, 'VALUE_TYPE' => 'WORK']; } @@ -139,7 +154,7 @@ class b24Handler extends FormHandler { } if (!empty($updateFields)) { - $updateResponse = b24_request('crm.contact.update', [ + $updateResponse = $this->b24_request('crm.contact.update', [ 'id' => $contactId, 'fields' => $updateFields ]); @@ -179,7 +194,7 @@ class b24Handler extends FormHandler { $newContact['fields']['EMAIL'][] = ['VALUE' => $email, 'VALUE_TYPE' => 'WORK']; } if (!empty($utm)) { - $newContact['fields']['UTM_SOURCE'] = $utm; + $newContact['fields']['UTM_SOURCE'] = $utm_source; } if (!empty($userID)) { $newContact['fields']['UF_CRM_1744373080655'] = $userID; @@ -196,7 +211,7 @@ class b24Handler extends FormHandler { ]; } - $response = b24_request('crm.contact.add', $newContact); + $response = $this->b24_request('crm.contact.add', $newContact); $contactId = $response['result'] ?? null; $error = $response['error'] ?? null; } @@ -270,7 +285,7 @@ class b24Handler extends FormHandler { error_log('DEAL DATA: ' . json_encode($dealData)); // Отправляем данные в Bitrix24 - $newDealResponse = b24_request($method, $dealData); + $newDealResponse = $this->b24_request($method, $dealData); error_log('DEAL RESP: ' . json_encode($newDealResponse)); @@ -297,9 +312,9 @@ class b24Handler extends FormHandler { } // Function to add lead (wraps b24_get_or_create_contact and b24_send_lead) - function b24_add_lead($phone, $name, $email, $msg, $utm, $url, $stage, $fName, $order_total = 0, $userID = null, $method = 'crm.deal.add', $form_title, $isSubscribe = false, $order_id = '') { - $contact = $this->b24_get_or_create_contact($phone, $name, $email, $utm, $userID, $isSubscribe); - + function b24_add_lead($phone, $name, $email, $msg, $url, $stage, $fName, $order_total = 0, $userID = null, $method = 'crm.deal.add', $form_title, $isSubscribe = false, $order_id = '') { + $contact = $this->b24_get_or_create_contact($phone, $name, $email, $userID, $isSubscribe); + error_log(json_encode($contact)); if (!empty($contact['contact_id'])) { $contactId = $contact['contact_id']; } else { @@ -326,7 +341,7 @@ class b24Handler extends FormHandler { $tg_username = get_user_meta($userID, 'tg_username', true); // Ищем контакт по ID пользователя - $response = b24_request('crm.contact.list', [ + $response = $this->b24_request('crm.contact.list', [ 'filter' => ['UF_CRM_1744373080655' => $userID], 'select' => ['ID', 'NAME', 'PHONE', 'EMAIL', 'UF_CRM_1744373080655', 'IM', 'UF_CRM_1744562461053'] ]); @@ -386,7 +401,7 @@ class b24Handler extends FormHandler { } // Send update request - $updateResponse = b24_request('crm.contact.update', [ + $updateResponse = $this->b24_request('crm.contact.update', [ 'id' => $contactId, 'fields' => $updateFields ]); @@ -400,17 +415,17 @@ class b24Handler extends FormHandler { } class zohoHandler extends FormHandler { - public function handle($data) { + public function handle($data, $crmData) { // Логика отправки в HubSpot error_log("Отправка в Zoho: " . json_encode($data)); - return parent::handle($data); + return parent::handle($data, $crmData); } } class tgHandler extends FormHandler { - public function handle($data) { + public function handle($data, $crmData) { global $site_env; if ($site_env->site_region == 'ru'){ @@ -428,7 +443,7 @@ class tgHandler extends FormHandler { $this->sendToTelegram($botToken, $chatId, $message); // Вызываем следующий обработчик в цепочке - return parent::handle($data); + return parent::handle($data, $crmData); } // Функция для отправки данных в Telegram @@ -466,7 +481,7 @@ class tgHandler extends FormHandler { } class emailHandler extends FormHandler { - public function handle($data) { + public function handle($data, $crmData) { $to = 'pro@cosmopet.shop'; // Укажите email, на который нужно отправить данные $subject = 'Форма обработана'; // Тема письма @@ -478,12 +493,13 @@ class emailHandler extends FormHandler { wp_mail($to, $subject, $message, $headers); // Вызываем следующий обработчик в цепочке - return parent::handle($data); + return parent::handle($data, $crmData); } } class FormHandlerFactory { public static function getHandler(array $enabledHandlers) { + global $site_env; $handler = null; if (in_array('email', $enabledHandlers)) { $handler = new emailHandler($handler); @@ -494,7 +510,7 @@ class FormHandlerFactory { if (in_array('tg', $enabledHandlers)) { $handler = new tgHandler($handler); } - if (in_array('b24', $enabledHandlers)) { + if (in_array('b24', $enabledHandlers) && $site_env->site_region == 'ru') { $handler = new b24Handler($handler); } return $handler ?: new DefaultHandler(); diff --git a/wp-content/themes/cosmopet/modules/popup/assets/js/modal.js b/wp-content/themes/cosmopet/modules/popup/assets/js/modal.js index d9e9ca4..02fe353 100644 --- a/wp-content/themes/cosmopet/modules/popup/assets/js/modal.js +++ b/wp-content/themes/cosmopet/modules/popup/assets/js/modal.js @@ -14,30 +14,30 @@ modal.onclick = function (event) { } } -// function -function modalOpen(buttonElement, contentElement) { +function modalOpen(buttonSelector, contentSelector) { let modal = document.querySelector('.modal'), aside = document.querySelector('.modal__aside'), - elements = document.querySelectorAll(buttonElement), body = document.querySelector('body'); - elements.forEach(e => { - let thisContentElement = document.querySelector(contentElement); + document.addEventListener('click', function (event) { + let target = event.target.closest(buttonSelector); // поддержка вложенных элементов (например, внутри ) + if (target) { + let thisContentElement = document.querySelector(contentSelector); - e.onclick = function () { body.classList.add('overflow-hidden'); modal.classList.add('active'); - aside.classList.add('active'); // Активируем aside для анимации + aside.classList.add('active'); thisContentElement.classList.add('active'); setTimeout(() => { thisContentElement.style.opacity = 1; thisContentElement.style.filter = 'blur(0px)'; }, 10); - }; + } }); } + function closeAllModals() { let modal = document.querySelector('.modal'), aside = document.querySelector('.modal__aside'), diff --git a/wp-content/themes/cosmopet/templates/modal/to_know.twig b/wp-content/themes/cosmopet/templates/modal/to_know.twig index f057874..da9bee4 100644 --- a/wp-content/themes/cosmopet/templates/modal/to_know.twig +++ b/wp-content/themes/cosmopet/templates/modal/to_know.twig @@ -11,7 +11,7 @@