Интеграция Битрикс 24 и Contact Form 7
add_action('wpcf7_mail_sent', 'b24wpcf7_wpcf7_mail_sent_function');
function b24wpcf7_wpcf7_mail_sent_function($contact_form) {
$title = $contact_form->title;
$posted_data = $contact_form->posted_data;
//Проверка id формы
if (8 == $contact_form->id()) {
$submission = WPCF7_Submission::get_instance();
$posted_data = $submission->get_posted_data();
$f7Name = $posted_data['text-527'];
$f7Tel = $posted_data['tel-348'];
if( !$utm_source = $posted_data['cf7_utm_source'] ) {
$utm_source = "yoursite";
}
//Адрес нужно получить в панели управления битрикса - хуки
$queryUrl = 'https://ваш!!!.bitrix24.ru/rest/1/ваш!!!/crm.lead.add.json';
$queryData = http_build_query(array(
'fields' => array(
'SOURCE_ID' => 12,
'TITLE' => $title.' | yoursite.ru',
'NAME' => $f7Name,
'UTM_SOURCE' => $USOURCE,
'PHONE' => array(
array(
'VALUE' => $f7Tel,
'VALUE_TYPE' => 'WORK'
)
)
) ,
'params' => array(
"REGISTER_SONET_EVENT" => "Y"
)
));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $queryUrl,
CURLOPT_POSTFIELDS => $queryData
));
$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result, 1);
if (array_key_exists('error', $result)) echo "Ошибка при сохранении лида: " . $result['error_description'] . "<br/>";
}
}
Интеграция Битрикс 24 и Elementor
Вариант кода с несколькими формами
<?php
add_action( 'elementor_pro/forms/new_record', function( $record, $handler ) {
//make sure its our form
$form_name = $record->get_form_settings( 'form_name' );
if ( 'Новая форма' == $form_name ) {
$raw_fields = $record->get( 'fields' );
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
$field_name = $fields['name'];
$field_mail = $fields['email'];
$UTM_SOURCE = $fields['UTM_SOURCE'];
if( $UTM_SOURCE ) {
$USOURCE = $fields['UTM_SOURCE'];
} else {
$UTM_SOURCE = "yoursite";
}
$queryUrl = 'https://ваш!!!.bitrix24.ru/rest/1/ваш!!!/crm.lead.add.json';
$queryData = http_build_query(array(
'fields' => array(
'SOURCE_ID' => 12,
'TITLE' => $form_name.' | yoursite.ru',
'NAME' => $field_name,
'EMAIL' => array(
array(
'VALUE' => $field_mail,
'VALUE_TYPE' => 'WORK'
)
),
'UTM_SOURCE' => $UTM_SOURCE,
) ,
'params' => array(
"REGISTER_SONET_EVENT" => "Y"
)
));
}
if ( 'order' == $form_name ) {
$raw_fields = $record->get( 'fields' );
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
$field_name = $fields['field_1'];
$field_tel = $fields['field_2'];
$field_city = $fields['field_3'];
if( !$utm_source = $fields['UTM_SOURCE'] ) {
$utm_source = "yoursite";
}
$queryUrl = 'https://ваш!!!.bitrix24.ru/rest/1/ваш!!!/crm.lead.add.json';
$queryData = http_build_query(array(
'fields' => array(
'SOURCE_ID' => 12,
'TITLE' => $form_name.' | yoursite.ru',
'NAME' => $field_name,
'PHONE' => array(
array(
'VALUE' => $field_tel,
'VALUE_TYPE' => 'WORK'
)
),
'UF_CRM_1580497756409' => $field_city,
'UTM_SOURCE' => $utm_source,
) ,
'params' => array(
"REGISTER_SONET_EVENT" => "Y"
)
));
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $queryUrl,
CURLOPT_POSTFIELDS => $queryData
));
$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result, 1);
$fd = fopen("b24log.txt", 'w') or die("не удалось создать файл");
$str = "Ошибка при сохранении лида: " . $result['error_description'] . "<br/>";
fwrite($fd, $str);
fclose($fd);
if (array_key_exists('error', $result)){
}
}, 10, 2);
Интеграция Битрикс 24 и WooCommerce
В коде используются произвольные поля тип UF_CRM_1603826565504. Я их оставил для примера. Их нужно удалить или заменить своими.
<?php
add_action('woocommerce_init', 'woocommerce_init_function22');
function woocommerce_init_function22() {
add_action('woocommerce_checkout_update_order_meta', 'b24wpcf7_action_function_name');
}
function b24wpcf7_action_function_name($order_id) {
if (!$order = wc_get_order($order_id)) {
return;
}
$order_data = $order->get_data(); // The Order data
$order_items = $order->get_items();
$order_shipping_methods = $order->get_shipping_methods();
$order_billing_first_name = $order->get_billing_first_name();
$order_billing_last_name = $order->get_billing_last_name();
$order_billing_email = $order->get_billing_email();
$order_billing_phone = $order->get_billing_phone();
$order_billing_city = $order->get_billing_city();
$order_shipping_total = $order_data['shipping_total'];
$order_customer_note = $order_data['customer_note'];
$order_shipping_address_1 = $order_data['shipping']['address_1'];
$W_UTM_SOURCE = get_post_meta( $order_id, 'UTM Source', true );
if( $W_UTM_SOURCE ) {
$W_UTM_SOURCE = get_post_meta( $order_id, 'UTM Source', true );
} else {
$W_UTM_SOURCE = "yoursite";
}
$product_details = array();
$shipping_details = array();
foreach ($order_items as $product) {
$product = $product->get_data();
$product_details[] = $product['name'].' x '.$product['quantity'];
}
$product_list = implode( ',', $product_details );
// Iterating through order shipping items
foreach($order_shipping_methods as $shipping_item) {
$shipping_item = $shipping_item->get_data();
$shipping_details[] = $shipping_item['method_title'];
}
$shipping_list = implode( ',', $shipping_details);
$queryUrl = 'https://ваш!!!.bitrix24.ru/rest/1/ваш!!!/crm.lead.add.json';
$queryData = http_build_query(array(
'fields' => array(
'SOURCE_ID' => 12,
'TITLE' => 'Заказ № '.$order_id.' | ИМ yoursite.ru',
'OPPORTUNITY' => $order_shipping_total,
'NAME' => $order_billing_first_name,
'PHONE' => array(
array(
'VALUE' => $order_billing_phone,
'VALUE_TYPE' => 'WORK'
)
),
'EMAIL' => array(
array(
'VALUE' => $order_billing_email,
'VALUE_TYPE' => 'WORK'
)
),
'COMMENTS' => $order_customer_note,
'UTM_SOURCE' => $W_UTM_SOURCE,
'UF_CRM_1603826565504' => $product_details,
'UF_CRM_1580497756409' => $order_billing_city, // город
'UF_CRM_1603876539733' => $shipping_list,
'UF_CRM_1603921324780' => $order_shipping_address_1
) ,
'params' => array(
"REGISTER_SONET_EVENT" => "Y"
)
));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $queryUrl,
CURLOPT_POSTFIELDS => $queryData
));
$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result, 1);
}