33 public static function catchCatalogBasket(
Event $event)
44 if (!static::getBxUserId())
60 $realProductId = $basketItem->getProductId();
61 $isCatalog = $basketItem->getField(
'MODULE') ==
'catalog';
65 $productInfo = \CCatalogSKU::GetProductInfo($realProductId);
67 if (!empty($productInfo[
'ID']))
69 $realProductId = $productInfo[
'ID'];
70 $iblockId = $productInfo[
'IBLOCK_ID'];
75 $element = \Bitrix\Iblock\ElementTable::getRow(array(
76 'select' => array(
'IBLOCK_ID'),
77 'filter' => array(
'=ID' => $realProductId)
82 $iblockId = $element[
'IBLOCK_ID'];
89 $recommendationId =
'';
92 $recommendationCookie =
Context::getCurrent()->getRequest()->getCookie(static::getCookieLogName());
94 if (!empty($recommendationCookie))
96 $recommendations = static::decodeProductLog($recommendationCookie);
98 if (is_array($recommendations) && isset($recommendations[$realProductId]))
100 $recommendationId = $recommendations[$realProductId][0];
104 if (empty($recommendationId) && $isCatalog)
108 if (!is_numeric($basketItem->getFUserId()))
110 $filter = array(
'CODE' => $basketItem->getFUserId());
114 $filter = array(
'ID' => $basketItem->getFUserId());
117 $result = \CSaleUser::getList($filter);
121 $siteUserId = $result[
'USER_ID'];
124 $fuser = $result[
'ID'];
126 $viewResult = CatalogViewedProductTable::getList(array(
127 'select' => array(
'RECOMMENDATION'),
129 '=FUSER_ID' => $fuser,
130 '=PRODUCT_ID' => $basketItem->getProductId()
132 'order' => array(
'DATE_VISIT' =>
'DESC')
135 if (!empty($viewResult[
'RECOMMENDATION']))
137 $recommendationId = $viewResult[
'RECOMMENDATION'];
144 'product_id' => $realProductId,
145 'iblock_id' => $iblockId,
146 'user_id' => $siteUserId,
147 'bx_user_id' => static::getBxUserId(),
149 'recommendation' => $recommendationId,
150 'date' => date(DATE_ISO8601)
156 $data[
'real_user_id'] = $USER->getId() ?: 0;
157 $data[
'is_admin'] = (int) $USER->IsAdmin();
158 $data[
'admin_section'] = (int) (defined(
'ADMIN_SECTION') && ADMIN_SECTION);
159 $data[
'admin_panel'] = (int) \CTopPanel::shouldShowPanel();
162 $data[
'artificial_basket'] = (int) (
163 ($data[
'user_id'] > 0 && $data[
'user_id'] != $data[
'real_user_id'])
164 || $data[
'is_admin'] || $data[
'admin_section'] || $data[
'admin_panel']
168 CounterDataTable::add(array(
174 if (!empty($recommendationId))
177 $helper = $conn->getSqlHelper();
180 "UPDATE ".$helper->quote(
'b_sale_basket')
181 .
" SET RECOMMENDATION='".$helper->forSql($recommendationId).
"' WHERE ID=".(
int) $basketItem->getId()
187 public static function catchCatalogOrder(
Event $event)
205 $data = static::getOrderInfo($orderItem->getId());
207 if (empty($data[
'products']))
213 $data[
'bx_user_id'] = static::getBxUserId();
215 if (empty($data[
'bx_user_id']) && !empty($data[
'user_id']))
218 'select' => array(
'BX_USER_ID'),
219 'filter' => array(
'=ID' => $data[
'user_id'])
222 if (!empty($orderUser) && !empty($orderUser[
'BX_USER_ID']))
224 $data[
'bx_user_id'] = $orderUser[
'BX_USER_ID'];
231 $data[
'date'] = date(DATE_ISO8601);
236 $data[
'real_user_id'] = $USER->getId() ?: 0;
237 $data[
'cookie_size'] = count($_COOKIE);
238 $data[
'is_admin'] = (int) $USER->IsAdmin();
239 $data[
'admin_section'] = (int) (defined(
'ADMIN_SECTION') && ADMIN_SECTION);
240 $data[
'admin_panel'] = (int) \CTopPanel::shouldShowPanel();
243 $data[
'artificial_order'] = (int) (
244 ($data[
'user_id'] != $data[
'real_user_id']) || !$data[
'cookie_size']
245 || $data[
'is_admin'] || $data[
'admin_section'] || $data[
'admin_panel']
248 CounterDataTable::add(array(
254 if (!empty($data[
'bx_user_id']))
257 if (OrderTable::getEntity()->hasField(
'BX_USER_ID'))
259 OrderTable::update($data[
'order_id'], array(
'BX_USER_ID' => $data[
'bx_user_id']));
265 public static function catchCatalogOrderPayment(Event $event)
273 $orderItem = $event->getParameter(
"ENTITY");
275 $data = static::getOrderInfo($orderItem->getId());
277 if (empty($data[
'products']))
283 $data[
'bx_user_id'] = static::getBxUserId();
285 if (empty($data[
'bx_user_id']) && OrderTable::getEntity()->hasField(
'BX_USER_ID'))
287 $order = OrderTable::getRow(array(
288 'select' => array(
'BX_USER_ID'),
289 'filter' => array(
'=ID' => $orderItem->getId())
292 if (!empty($order) && !empty($order[
'BX_USER_ID']))
294 $data[
'bx_user_id'] = $order[
'BX_USER_ID'];
301 $data[
'date'] = date(DATE_ISO8601);
303 CounterDataTable::add(array(
304 'TYPE' =>
'order_pay',
312 $order = \CSaleOrder::getById($orderId);
315 $siteUserId = $order[
'USER_ID'];
324 $result = \CSaleOrderPropsValue::GetList(array(), array(
"ORDER_ID" => $orderId));
325 while ($row = $result->fetch())
327 if (empty($phone) && mb_stripos($row[
'CODE'],
'PHONE') !==
false)
329 $stPhone = static::normalizePhoneNumber($row[
'VALUE']);
331 if (!empty($stPhone))
333 $phone = sha1($stPhone);
334 $phone256 = hash(
'sha256', $stPhone);
335 $phone256_e164 = hash(
'sha256',
'+'.$stPhone);
339 if (empty($email) && mb_stripos($row[
'CODE'],
'EMAIL') !==
false)
341 if (!empty($row[
'VALUE']))
343 $email = sha1($row[
'VALUE']);
344 $email256 = hash(
'sha256', mb_strtolower(trim($row[
'VALUE'])));
352 $result = \CSaleBasket::getList(
353 array(), $arFilter = array(
'ORDER_ID' => $orderId),
false,
false,
354 array(
'PRODUCT_ID',
'RECOMMENDATION',
'QUANTITY',
'PRICE',
'CURRENCY',
'MODULE')
357 while ($row = $result->fetch())
359 $realProductId = $row[
'PRODUCT_ID'];
363 if ($row[
'MODULE'] ==
'catalog')
365 $productInfo = \CCatalogSKU::GetProductInfo($row[
'PRODUCT_ID']);
367 if (!empty($productInfo[
'ID']))
369 $realProductId = $productInfo[
'ID'];
370 $iblockId = $productInfo[
'IBLOCK_ID'];
375 $element = \Bitrix\Iblock\ElementTable::getRow(array(
376 'select' => array(
'IBLOCK_ID'),
377 'filter' => array(
'=ID' => $realProductId)
380 if (!empty($element))
382 $iblockId = $element[
'IBLOCK_ID'];
388 'product_id' => $realProductId,
389 'iblock_id' => $iblockId,
390 'quantity' => $row[
'QUANTITY'],
391 'price' => $row[
'PRICE'],
392 'currency' => $row[
'CURRENCY'],
393 'recommendation' => $row[
'RECOMMENDATION']
399 'order_id' => $orderId,
400 'user_id' => $siteUserId,
402 'phone256' => $phone256,
403 'phone256_e164' => $phone256_e164,
405 'email256' => $email256,
406 'products' => $products,
407 'price' => $order[
'PRICE'],
408 'currency' => $order[
'CURRENCY']
416 return $_COOKIE[
'BX_USER_ID'];
421 $phone = preg_replace(
'/[^\d]/',
'', $phone);
423 $cleanPhone = \NormalizePhone($phone, 6);
425 if (mb_strlen($cleanPhone) == 10)
427 $cleanPhone =
'7'.$cleanPhone;
436 && Option::get(
"main",
"gather_catalog_stat",
"Y") ===
"Y"
443 if (empty($offerIds))
447 $iblockGroup = array();
448 $itemIterator = \Bitrix\Iblock\ElementTable::getList(array(
449 'select' => array(
'ID',
'IBLOCK_ID'),
450 'filter' => array(
'@ID' => $offerIds,
'=ACTIVE'=>
'Y')
452 while ($item = $itemIterator->fetch())
454 if (!isset($iblockGroup[$item[
'IBLOCK_ID']]))
455 $iblockGroup[$item[
'IBLOCK_ID']] = array();
456 $iblockGroup[$item[
'IBLOCK_ID']][] = $item[
'ID'];
457 $bestList[$item[
'ID']] = array();
460 if (empty($iblockGroup))
465 $iblockSku = array();
466 $iblockOffers = array();
468 $iblockIterator = \Bitrix\Catalog\CatalogIblockTable::getList(array(
469 'select' => array(
'IBLOCK_ID',
'PRODUCT_IBLOCK_ID',
'SKU_PROPERTY_ID',
'VERSION' =>
'IBLOCK.VERSION'),
470 'filter' => array(
'=IBLOCK_ID' => array_keys($iblockGroup),
'!=PRODUCT_IBLOCK_ID' => 0)
472 while ($iblock = $iblockIterator->fetch())
474 $iblock[
'IBLOCK_ID'] = (int)$iblock[
'IBLOCK_ID'];
475 $iblock[
'PRODUCT_IBLOCK_ID'] = (int)$iblock[
'PRODUCT_IBLOCK_ID'];
476 $iblock[
'SKU_PROPERTY_ID'] = (int)$iblock[
'SKU_PROPERTY_ID'];
477 $iblock[
'VERSION'] = (int)$iblock[
'VERSION'];
478 $iblockSku[$iblock[
'IBLOCK_ID']] = $iblock;
479 $iblockOffers[$iblock[
'IBLOCK_ID']] = $iblockGroup[$iblock[
'IBLOCK_ID']];
481 unset($iblock, $iblockIterator);
483 if (empty($iblockOffers))
486 $offerLink = array();
487 foreach ($iblockOffers as $iblockId => $items)
489 $skuProperty =
'PROPERTY_'.$iblockSku[$iblockId][
'SKU_PROPERTY_ID'];
490 $iblockFilter = array(
491 'IBLOCK_ID' => $iblockId,
494 $iblockFields = array(
'ID',
'IBLOCK_ID', $skuProperty);
495 $skuProperty .=
'_VALUE';
496 $offersIterator = \CIBlockElement::getList(
497 array(
'ID' =>
'ASC'),
504 while ($offer = $offersIterator->Fetch())
506 $productId = (int)$offer[$skuProperty];
509 unset($bestList[$offer[
'ID']]);
513 $bestList[$offer[
'ID']][
'PARENT_ID'] = $productId;
514 $bestList[$offer[
'ID']][
'PARENT_IBLOCK'] = $iblockSku[$iblockId][
'PRODUCT_IBLOCK_ID'];
515 if (!isset($offerLink[$productId]))
516 $offerLink[$productId] = array();
517 $offerLink[$productId][] = $offer[
'ID'];
521 if (!empty($offerLink))
523 $productIterator = \Bitrix\Iblock\ElementTable::getList(array(
524 'select' => array(
'ID'),
525 'filter' => array(
'@ID' => array_keys($offerLink),
'=ACTIVE' =>
'N')
527 while ($product = $productIterator->fetch())
529 if (empty($offerLink[$product[
'ID']]))
531 foreach ($offerLink[$product[
'ID']] as $value)
533 unset($bestList[$value]);
538 if (empty($bestList))
542 $dublicate = array();
543 foreach ($bestList as $id => $info)
547 if (!isset($dublicate[$id]))
549 $dublicate[$id] =
true;
553 if (!isset($dublicate[$id]))
554 $finalIds[] = $info[
'PARENT_ID'];
555 $dublicate[$info[
'PARENT_ID']] =
true;
558 unset($id, $info, $dublicate);
572 foreach ($log as $itemId => $recommendation)
574 $rcmId = $recommendation[0];
575 $rcmTime = $recommendation[1];
577 $value[] = $itemId.
'-'.$rcmId.
'-'.$rcmTime;
580 return join(
'.', $value);
591 $tmp = explode(
'.', $log);
593 foreach ($tmp as $tmpval)
595 $meta = explode(
'-', $tmpval);
597 if (count($meta) > 2)
603 if ($itemId && $rcmId && $rcmTime)
605 $value[(int)$itemId] = array($rcmId, (
int) $rcmTime);
static normalizePhoneNumber($phone)
static getProductIdsByOfferIds($offerIds)
static encodeProductLog(array $log)
static getCookieLogName()
static decodeProductLog($log)
static getOrderInfo($orderId)
static getConnection($name="")
static includeModule($moduleName)
static getRow(array $parameters)