33 public static function catchCatalogBasket(
Event $event)
35 $isNew =
$event->getParameter(
"IS_NEW");
44 if (!static::getBxUserId())
56 $basketItem =
$event->getParameter(
"ENTITY");
60 $realProductId = $basketItem->getProductId();
61 $isCatalog = $basketItem->getField(
'MODULE') ==
'catalog';
65 $productInfo = \CCatalogSKU::GetProductInfo($realProductId);
67 if (!empty($productInfo[
'ID']))
69 $realProductId = $productInfo[
'ID'];
76 'select' =>
array(
'IBLOCK_ID'),
77 'filter' =>
array(
'=ID' => $realProductId)
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()))
121 $siteUserId =
$result[
'USER_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,
146 'user_id' => $siteUserId,
147 'bx_user_id' => static::getBxUserId(),
148 'domain' => Context::getCurrent()->getServer()->getHttpHost(),
149 'recommendation' => $recommendationId,
150 'date' => date(DATE_ISO8601)
162 $data[
'artificial_basket'] = (int) (
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)
194 $isNew =
$event->getParameter(
"IS_NEW");
203 $orderItem =
$event->getParameter(
"ENTITY");
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'),
222 if (!empty($orderUser) && !empty($orderUser[
'BX_USER_ID']))
224 $data[
'bx_user_id'] = $orderUser[
'BX_USER_ID'];
230 $data[
'domain'] = Context::getCurrent()->getServer()->getHttpHost();
231 $data[
'date'] = date(DATE_ISO8601);
243 $data[
'artificial_order'] = (int) (
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'))
288 'select' =>
array(
'BX_USER_ID'),
289 'filter' =>
array(
'=ID' => $orderItem->getId())
300 $data[
'domain'] = Context::getCurrent()->getServer()->getHttpHost();
301 $data[
'date'] = date(DATE_ISO8601);
304 'TYPE' =>
'order_pay',
315 $siteUserId =
$order[
'USER_ID'];
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(
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'];
376 'select' =>
array(
'IBLOCK_ID'),
377 'filter' =>
array(
'=ID' => $realProductId)
380 if (!empty($element))
388 'product_id' => $realProductId,
390 'quantity' => $row[
'QUANTITY'],
391 'price' => $row[
'PRICE'],
392 'currency' => $row[
'CURRENCY'],
393 'recommendation' => $row[
'RECOMMENDATION']
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);
425 if (mb_strlen($cleanPhone) == 10)
427 $cleanPhone =
'7'.$cleanPhone;
440 if (empty($offerIds))
444 $iblockGroup =
array();
446 'select' =>
array(
'ID',
'IBLOCK_ID'),
447 'filter' =>
array(
'@ID' => $offerIds,
'=ACTIVE'=>
'Y')
449 while ($item = $itemIterator->fetch())
451 if (!isset($iblockGroup[$item[
'IBLOCK_ID']]))
452 $iblockGroup[$item[
'IBLOCK_ID']] =
array();
453 $iblockGroup[$item[
'IBLOCK_ID']][] = $item[
'ID'];
454 $bestList[$item[
'ID']] =
array();
457 if (empty($iblockGroup))
462 $iblockSku =
array();
463 $iblockOffers =
array();
466 'select' =>
array(
'IBLOCK_ID',
'PRODUCT_IBLOCK_ID',
'SKU_PROPERTY_ID',
'VERSION' =>
'IBLOCK.VERSION'),
467 'filter' =>
array(
'=IBLOCK_ID' => array_keys($iblockGroup),
'!=PRODUCT_IBLOCK_ID' => 0)
469 while (
$iblock = $iblockIterator->fetch())
476 $iblockOffers[
$iblock[
'IBLOCK_ID']] = $iblockGroup[
$iblock[
'IBLOCK_ID']];
478 unset(
$iblock, $iblockIterator);
480 if (empty($iblockOffers))
483 $offerLink =
array();
486 $skuProperty =
'PROPERTY_'.$iblockSku[
$iblockId][
'SKU_PROPERTY_ID'];
487 $iblockFilter =
array(
491 $iblockFields =
array(
'ID',
'IBLOCK_ID', $skuProperty);
492 $skuProperty .=
'_VALUE';
493 $offersIterator = \CIBlockElement::getList(
494 array(
'ID' =>
'ASC'),
501 while ($offer = $offersIterator->Fetch())
503 $productId = (int)$offer[$skuProperty];
506 unset($bestList[$offer[
'ID']]);
510 $bestList[$offer[
'ID']][
'PARENT_ID'] = $productId;
511 $bestList[$offer[
'ID']][
'PARENT_IBLOCK'] = $iblockSku[
$iblockId][
'PRODUCT_IBLOCK_ID'];
512 if (!isset($offerLink[$productId]))
513 $offerLink[$productId] =
array();
514 $offerLink[$productId][] = $offer[
'ID'];
518 if (!empty($offerLink))
521 'select' =>
array(
'ID'),
522 'filter' =>
array(
'@ID' => array_keys($offerLink),
'=ACTIVE' =>
'N')
524 while ($product = $productIterator->fetch())
526 if (empty($offerLink[$product[
'ID']]))
528 foreach ($offerLink[$product[
'ID']] as $value)
530 unset($bestList[$value]);
535 if (empty($bestList))
539 $dublicate =
array();
540 foreach ($bestList as $id =>
$info)
544 if (!isset($dublicate[$id]))
546 $dublicate[$id] =
true;
550 if (!isset($dublicate[$id]))
551 $finalIds[] =
$info[
'PARENT_ID'];
552 $dublicate[
$info[
'PARENT_ID']] =
true;
555 unset($id,
$info, $dublicate);
569 foreach ($log as $itemId => $recommendation)
571 $rcmId = $recommendation[0];
572 $rcmTime = $recommendation[1];
574 $value[] = $itemId.
'-'.$rcmId.
'-'.$rcmTime;
577 return join(
'.', $value);
588 $tmp = explode(
'.', $log);
590 foreach (
$tmp as $tmpval)
592 $meta = explode(
'-', $tmpval);
594 if (
count($meta) > 2)
600 if ($itemId && $rcmId && $rcmTime)
602 $value[(int)$itemId] =
array($rcmId, (
int) $rcmTime);
615 return self::$cookieLogName;
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)
static getList(array $parameters=array())