Task 6989 | исправил логику открытия модальных окон (не открывались в ajax загруженных карточках), дописал и проверил логику отправки в b24

pull/36/head
parent e1e7a5e718
commit de3195c897
  1. 140
      wp-content/themes/cosmopet/modules/forms/module-ajax-controller.php
  2. 74
      wp-content/themes/cosmopet/modules/forms/module-controller.php
  3. 16
      wp-content/themes/cosmopet/modules/popup/assets/js/modal.js
  4. 2
      wp-content/themes/cosmopet/templates/modal/to_know.twig

@ -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);
}

@ -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();

@ -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); // поддержка вложенных элементов (например, <span> внутри <a>)
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'),

@ -11,7 +11,7 @@
<form action="to_know_form" class="modal__form-sub">
<div class="label">
<label for="mail" class="label__title">
{{ fn ('pll_e', 'mail') }}
{{ fn ('pll_e', 'Эл.почта') }}
</label>
</div>
<input type="hidden" name="sub_product" value="" id="sub_product_name">

Loading…
Cancel
Save