B
B
Brepex2022-03-17 12:22:33
PHP
Brepex, 2022-03-17 12:22:33

How to get the id of a lead created through the WooCommerce plugin (wooCommerce integration with bitrix)?

Good day, fellow programmers, I have such a problem
Task: Integrate the wooCommerce plugin with Bitrix 24, the point is that when a user buys on the site, the plugin must create a lead in Bitrix and fill in the "Product" field.
Essence: There is a ready-made script that creates a lead in Bitrix, everything works OK, but I also need to attach the right product to the lead and the problem is that just entering it in the field when creating it does not work, you need to use the crm.lead.productrows method. set, and in order to use this method, you need a lead id, and the question is how can I get the id of the woocommerce-created lead, so that I can attach the product to the lead with a hook
. I would be very grateful!
Here is the script

add_action( ‘woocommerce_thankyou’, ‘my_custom_tracking’ );

function my_custom_tracking( $order_id ) {

  // Подключаемся к серверу CRM

  define(‘CRM_HOST’, ‘[ваше_название].bitrix24.ru’); // Ваш домен CRM системы

  define(‘CRM_PORT’, ‘443’); // Порт сервера CRM. Установлен по умолчанию

  define(‘CRM_PATH’, ‘/crm/configs/import/lead.php’); // Путь к компоненту lead.rest

  // Авторизуемся в CRM под необходимым пользователем:

  // 1. Указываем логин пользователя Вашей CRM по управлению лидами

  define(‘CRM_LOGIN’, ‘[email protected]’);

  // 2. Указываем пароль пользователя Вашей CRM по управлению лидами

  define(‘CRM_PASSWORD’, ‘your_password’);

  // Получаем информации по заказу

  $order = wc_get_order( $order_id );

  $order_data = $order->get_data();

  // Получаем базовую информация по заказу

  $order_id = $order_data[‘id’];

  $order_currency = $order_data[‘currency’];

  $order_payment_method_title = $order_data[‘payment_method_title’];

  $order_shipping_totale = $order_data[‘shipping_total’];

  $order_total = $order_data[‘total’];

  $order_base_info = «<hr><strong>Общая информация по заказу</strong><br>

  ID заказа: $order_id<br>

  Валюта заказа: $order_currency<br>

  Метода оплаты: $order_payment_method_title<br>

  Стоимость доставки: $order_shipping_totale<br>

  Итого с доставкой: $order_total<br>»;

  // Получаем информация по клиенту

  $order_customer_id = $order_data[‘customer_id’];

  $order_customer_ip_address = $order_data[‘customer_ip_address’];

  $order_billing_first_name = $order_data[‘billing’][‘first_name’];

  $order_billing_last_name = $order_data[‘billing’][‘last_name’];

  $order_billing_email = $order_data[‘billing’][’email’];

  $order_billing_phone = $order_data[‘billing’][‘phone’];

  $order_client_info = «<hr><strong>Информация по клиенту</strong><br>

  ID клиента = $order_customer_id<br>

  IP адрес клиента: $order_customer_ip_address<br>

  Имя клиента: $order_billing_first_name<br>

  Фамилия клиента: $order_billing_last_name<br>

  Email клиента: $order_billing_email<br>

  Телефон клиента: $order_billing_phone<br>»;

  // Получаем информацию по доставке

  $order_shipping_address_1 = $order_data[‘shipping’][‘address_1’];

  $order_shipping_address_2 = $order_data[‘shipping’][‘address_2’];

  $order_shipping_city = $order_data[‘shipping’][‘city’];

  $order_shipping_state = $order_data[‘shipping’][‘state’];

  $order_shipping_postcode = $order_data[‘shipping’][‘postcode’];

  $order_shipping_country = $order_data[‘shipping’][‘country’];

  $order_shipping_info = «<hr><strong>Информация по доставке</strong><br>

  Страна доставки: $order_shipping_state<br>

  Город доставки: $order_shipping_city<br>

  Индекс: $order_shipping_postcode<br>

  Адрес доставки 1: $order_shipping_address_1<br>

  Адрес доставки 2: $order_shipping_address_2<br>»;

  // Получаем информации по товару

  $order->get_total();

  $line_items = $order->get_items();

  foreach ( $line_items as $item ) {

    $product = $order->get_product_from_item( $item );

    $sku = $product->get_sku(); // артикул товара

    $id = $product->get_id(); // id товара

    $name = $product->get_name(); // название товара

    $description = $product->get_description(); // описание товара

    $stock_quantity = $product->get_stock_quantity(); // кол-во товара на складе

    $qty = $item[‘qty’]; // количество товара, которое заказали

    $total = $order->get_line_total( $item, true, true ); // стоимость всех товаров, которые заказали, но без учета доставки

    $product_info[] = «<hr><strong>Информация о товаре</strong><br>

    Название товара: $name<br>

    ID товара: $id<br>

    Артикул: $sku<br>

    Описание: $description<br>

    Заказали (шт.): $qty<br>

    Наличие (шт.): $stock_quantity<br>

    Сумма заказа (без учета доставки): $total;»;

  }

  $product_base_infо = implode(‘<br>’, $product_info);

  $subject = «Заказ с сайта № $order_id»;

  // Формируем параметры для создания лида в переменной $postData = array

  $postData = array(

    ‘TITLE’ => $subject,

    ‘COMMENTS’ => $order_base_info.’ ‘.$order_client_info.’ ‘.$order_shipping_info.’ ‘.$product_base_infо

  );

  // Передаем данные из Woocommerce в Bitrix24

  if (defined(‘CRM_AUTH’)) {

    $postData[‘AUTH’] = CRM_AUTH;

  } else {

    $postData[‘LOGIN’] = CRM_LOGIN;

    $postData[‘PASSWORD’] = CRM_PASSWORD;

  }

  $fp = fsockopen(«ssl://».CRM_HOST, CRM_PORT, $errno, $errstr, 30);

  if ($fp) {

    $strPostData = »;

    foreach ($postData as $key => $value)

    $strPostData .= ($strPostData == » ? » : ‘&’).$key.’=’.urlencode($value);

    $str = «POST «.CRM_PATH.» HTTP/1.0\r\n»;

    $str .= «Host: «.CRM_HOST.»\r\n»;

    $str .= «Content-Type: application/x-www-form-urlencoded\r\n»;

    $str .= «Content-Length: «.strlen($strPostData).»\r\n»;

    $str .= «Connection: close\r\n\r\n»;

    $str .= $strPostData;

    fwrite($fp, $str);

    $result = »;

    while (!feof($fp))

    {

      $result .= fgets($fp, 128);

    }

    fclose($fp);

    $response = explode(«\r\n\r\n», $result);

    $output = ‘<pre>’.print_r($response[1], 1).'</pre>’;

  } else {

    echo ‘Connection Failed! ‘.$errstr.’ (‘.$errno.’)’;

  }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikolaev, 2022-03-20
@Brepex

the question is how to get the id of the created lead

Replace the call of the archaic method with a login and password with the crm.lead.add webhook.
As a result of the call, it will return the ID of the created lead, which you can use to fill in the product items

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question