51 'autocomplete' =>
true,
52 'title' => Loc::getMessage(
'VIEWED_PRODUCT_ENTITY_ID_FIELD')
55 'title' => Loc::getMessage(
'VIEWED_PRODUCT_ENTITY_FUSER_ID_FIELD')
59 'title' => Loc::getMessage(
'VIEWED_PRODUCT_ENTITY_DATE_VISIT_FIELD')
61 'PRODUCT_ID' =>
new Main\Entity\IntegerField(
'PRODUCT_ID',
array(
62 'title' => Loc::getMessage(
'VIEWED_PRODUCT_ENTITY_PRODUCT_ID_FIELD')
64 'ELEMENT_ID' =>
new Main\Entity\IntegerField(
'ELEMENT_ID',
array(
65 'title' => Loc::getMessage(
'VIEWED_PRODUCT_ENTITY_ELEMENT_ID_FIELD')
67 'SITE_ID' =>
new Main\Entity\StringField(
'SITE_ID',
array(
68 'validation' =>
array(__CLASS__,
'validateSiteId'),
69 'title' => Loc::getMessage(
'VIEWED_PRODUCT_ENTITY_SITE_ID_FIELD')
71 'VIEW_COUNT' =>
new Main\Entity\IntegerField(
'VIEW_COUNT',
array(
72 'title' => Loc::getMessage(
'VIEWED_PRODUCT_ENTITY_VIEW_COUNT_FIELD')
74 'RECOMMENDATION' =>
new Main\Entity\StringField(
'RECOMMENDATION',
array(
75 'validation' =>
array(__CLASS__,
'validateRecommendation'),
76 'title' => Loc::getMessage(
'VIEWED_PRODUCT_ENTITY_RECOMMENDATION_FIELD')
78 'ELEMENT' =>
new Main\Entity\ReferenceField(
80 '\Bitrix\Iblock\Element',
81 array(
'=this.PRODUCT_ID' =>
'ref.ID'),
82 array(
'join_type' =>
'INNER')
84 'PRODUCT' =>
new Main\Entity\ReferenceField(
86 '\Bitrix\Sale\Internals\Product',
87 array(
'=this.PRODUCT_ID' =>
'ref.ID'),
88 array(
'join_type' =>
'INNER')
90 'PARENT_ELEMENT' =>
new Main\Entity\ReferenceField(
92 '\Bitrix\Iblock\Element',
93 array(
'=this.ELEMENT_ID' =>
'ref.ID'),
94 array(
'join_type' =>
'INNER')
96 'FUSER' =>
new Main\Entity\ReferenceField(
98 '\Bitrix\Sale\Internals\Fuser',
99 array(
'=this.FUSER_ID' =>
'ref.ID'),
100 array(
'join_type' =>
'LEFT')
144 if (Option::get(
'catalog',
'enable_viewed_products') !==
'Y')
149 $productId = (int)$productId;
150 $fuserId = (int)$fuserId;
152 $elementId = (int)$elementId;
153 $recommendationId = (string)$recommendationId;
154 if ($productId <= 0 || $fuserId <= 0 ||
$siteId ==
'')
165 $sqlSiteId = $helper->forSql(
$siteId);
169 $filter[
"=ELEMENT_ID"] = $elementId;
172 if ($elementId != $productId)
174 "delete from b_catalog_viewed_product where PRODUCT_ID = ".$elementId.
" and FUSER_ID = ".$fuserId.
" and SITE_ID = '".$sqlSiteId.
"'"
180 $productInfo = \CCatalogSku::getProductInfo($productId);
182 if (!empty($productInfo))
184 $elementId = $productInfo[
'ID'];
187 $connection->query(
"delete from b_catalog_viewed_product
188 where PRODUCT_ID = ".$productInfo[
'ID'].
" and FUSER_ID = ".$fuserId.
" and SITE_ID = '" .$sqlSiteId.
"'"
191 $skus = \CIBlockElement::getList(
193 array(
'IBLOCK_ID' => $productInfo[
'OFFER_IBLOCK_ID'],
'=PROPERTY_'.$productInfo[
'SKU_PROPERTY_ID'] => $productInfo[
'ID']),
196 array(
'ID',
'IBLOCK_ID')
199 while ($oneSku = $skus->fetch())
200 $siblings[] = $oneSku[
'ID'];
201 unset($oneSku, $skus);
203 $filter[
'@PRODUCT_ID'] = $siblings;
207 $elementId = $productId;
208 $filter[
'=PRODUCT_ID'] = $productId;
213 if (!empty($elementId))
218 if (!empty($recommendationCookie))
221 if (is_array($recommendations) && isset($recommendations[$elementId]))
222 $recommendationId = $recommendations[$elementId][0];
227 'select' =>
array(
'ID',
'FUSER_ID',
'DATE_VISIT',
'PRODUCT_ID',
'SITE_ID',
'VIEW_COUNT'),
234 "PRODUCT_ID" => $productId,
236 'VIEW_COUNT' => $row[
'VIEW_COUNT'] + 1,
237 "ELEMENT_ID" => $elementId
239 if (!empty($recommendationId))
240 $update[
"RECOMMENDATION"] = $recommendationId;
242 $result = static::update($row[
'ID'], $update);
247 "FUSER_ID" => $fuserId,
249 "PRODUCT_ID" => $productId,
250 "ELEMENT_ID" => $elementId,
253 "RECOMMENDATION" => $recommendationId
260 self::truncateUserViewedProducts($fuserId,
$siteId);
304 $sectionId = (int)$sectionId;
305 $fuserId = (int)$fuserId;
306 $excludeProductId = (int)$excludeProductId;
307 $limit = (int)$limit;
308 $depth = (int)$depth;
309 if (
$iblockId <= 0 || $depth < 0 || $fuserId <= 0)
314 $context = Application::getInstance()->getContext();
320 $subSections =
array();
324 if ($parentSectionId !==
null)
325 $subSections[$parentSectionId] = $parentSectionId;
326 unset($parentSectionId);
329 if (empty($subSections) && $sectionId <= 0)
332 'select' =>
array(
'PRODUCT_ID',
'ELEMENT_ID',
'DATE_VISIT'),
334 '=FUSER_ID' => $fuserId,
336 '=PARENT_ELEMENT.IBLOCK_ID' =>
$iblockId,
337 '=PARENT_ELEMENT.WF_STATUS_ID' => 1,
338 '=PARENT_ELEMENT.WF_PARENT_ELEMENT_ID' =>
null
340 'order' =>
array(
'DATE_VISIT' =>
'DESC')
342 if ($excludeProductId > 0)
343 $getListParams[
'filter'][
'!=PARENT_ELEMENT.ID'] = $excludeProductId;
351 if (empty($subSections))
352 $subSections[$sectionId] = $sectionId;
354 $sectionQuery =
new Main\Entity\Query(
Iblock\SectionTable::getEntity());
355 $sectionQuery->setTableAliasPostfix(
'_parent');
356 $sectionQuery->setSelect(
array(
'ID',
'LEFT_MARGIN',
'RIGHT_MARGIN'));
357 $sectionQuery->setFilter(
array(
'@ID' => $subSections));
359 $subSectionQuery =
new Main\Entity\Query(
Iblock\SectionTable::getEntity());
360 $subSectionQuery->setTableAliasPostfix(
'_sub');
361 $subSectionQuery->setSelect(
array(
'ID'));
363 $subSectionQuery->registerRuntimeField(
367 Main\
Entity\Base::getInstanceByQuery($sectionQuery),
368 array(
'>=this.LEFT_MARGIN' =>
'ref.LEFT_MARGIN',
'<=this.RIGHT_MARGIN' =>
'ref.RIGHT_MARGIN'),
369 array(
'join_type' =>
'INNER')
373 $sectionElementQuery =
new Main\Entity\Query(
Iblock\SectionElementTable::getEntity());
374 $sectionElementQuery->setSelect(
array(
'IBLOCK_ELEMENT_ID'));
375 $sectionElementQuery->setGroup(
array(
'IBLOCK_ELEMENT_ID'));
377 if ($excludeProductId > 0)
378 $filter[
'!=IBLOCK_ELEMENT_ID'] = $excludeProductId;
379 $sectionElementQuery->setFilter(
$filter);
381 $sectionElementQuery->registerRuntimeField(
385 Main\
Entity\Base::getInstanceByQuery($subSectionQuery),
386 array(
'=this.IBLOCK_SECTION_ID' =>
'ref.ID'),
387 array(
'join_type' =>
'INNER')
391 $elementQuery =
new Main\Entity\Query(
Iblock\ElementTable::getEntity());
392 $elementQuery->setSelect(
array(
'ID'));
394 if ($excludeProductId > 0)
395 $filter[
'!=ID'] = $excludeProductId;
396 $elementQuery->setFilter(
$filter);
398 $elementQuery->registerRuntimeField(
402 Main\
Entity\Base::getInstanceByQuery($sectionElementQuery),
403 array(
'=this.ID' =>
'ref.IBLOCK_ELEMENT_ID'),
404 array(
'join_type' =>
'INNER')
409 $query->setSelect(
array(
'PRODUCT_ID',
'ELEMENT_ID',
'DATE_VISIT'));
413 $query->registerRuntimeField(
417 Main\
Entity\Base::getInstanceByQuery($elementQuery),
418 array(
'=this.ELEMENT_ID' =>
'ref.ID'),
419 array(
'join_type' =>
'INNER')
427 unset(
$query, $elementQuery, $sectionElementQuery, $subSectionQuery, $sectionQuery);
431 $map[$row[
'PRODUCT_ID']] = $row[
'ELEMENT_ID'];