|
|
|
@ -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 '<script> |
|
|
|
|
// (window.b24order = window.b24order || []).push({ |
|
|
|
|
// id: "' . $order_id . '", |
|
|
|
|
// sum: "' . $order->get_total() . '" |
|
|
|
|
// }); |
|
|
|
|
// </script>'; |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
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(); |