140 public static function refresh($productId, $fuserId, $siteId = SITE_ID, $elementId = 0, $recommendationId =
'')
144 if (Option::get(
'catalog',
'enable_viewed_products') !==
'Y')
149 $productId = (int)$productId;
150 $fuserId = (int)$fuserId;
151 $siteId = (string)$siteId;
152 $elementId = (int)$elementId;
153 $recommendationId = (string)$recommendationId;
154 if ($productId <= 0 || $fuserId <= 0 || $siteId ==
'')
157 if (!Catalog\
Product\Basket::isNotCrawler())
160 $filter = array(
'=FUSER_ID' => $fuserId,
'=SITE_ID' => $siteId);
163 $helper = $connection->getSqlHelper();
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))
217 $recommendationCookie = $APPLICATION->get_cookie(Main\Analytics\Catalog::getCookieLogName());
218 if (!empty($recommendationCookie))
220 $recommendations = Main\Analytics\Catalog::decodeProductLog($recommendationCookie);
221 if (is_array($recommendations) && isset($recommendations[$elementId]))
222 $recommendationId = $recommendations[$elementId][0];
226 $iterator = static::getList(array(
227 'select' => array(
'ID',
'FUSER_ID',
'DATE_VISIT',
'PRODUCT_ID',
'SITE_ID',
'VIEW_COUNT'),
231 if ($row = $iterator->fetch())
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);
246 $result = static::add(array(
247 "FUSER_ID" => $fuserId,
249 "PRODUCT_ID" => $productId,
250 "ELEMENT_ID" => $elementId,
251 "SITE_ID" => $siteId,
253 "RECOMMENDATION" => $recommendationId
257 if ($result->isSuccess(
true))
259 $rowId = $result->getId();
260 self::truncateUserViewedProducts($fuserId, $siteId);
299 public static function getProductSkuMap($iblockId, $sectionId, $fuserId, $excludeProductId, $limit, $depth = 0, $siteId =
null)
303 $iblockId = (int)$iblockId;
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)
315 $siteId = $context->getSite();
320 $subSections = array();
323 $parentSectionId = Product\Viewed::getParentSection($sectionId, $depth);
324 if ($parentSectionId !==
null)
325 $subSections[$parentSectionId] = $parentSectionId;
326 unset($parentSectionId);
329 if (empty($subSections) && $sectionId <= 0)
331 $getListParams = array(
332 'select' => array(
'PRODUCT_ID',
'ELEMENT_ID',
'DATE_VISIT'),
334 '=FUSER_ID' => $fuserId,
335 '=SITE_ID' => $siteId,
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;
345 $getListParams[
'limit'] = $limit;
346 $iterator = static::getList($getListParams);
347 unset($getListParams);
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'));
362 $subSectionQuery->setFilter(array(
'=IBLOCK_ID' => $iblockId));
363 $subSectionQuery->registerRuntimeField(
365 new Main\
Entity\ReferenceField(
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'));
376 $filter = array(
'=ADDITIONAL_PROPERTY_ID' =>
null);
377 if ($excludeProductId > 0)
378 $filter[
'!=IBLOCK_ELEMENT_ID'] = $excludeProductId;
379 $sectionElementQuery->setFilter($filter);
381 $sectionElementQuery->registerRuntimeField(
383 new Main\
Entity\ReferenceField(
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'));
393 $filter = array(
'=IBLOCK_ID' => $iblockId,
'=WF_STATUS_ID' => 1,
'=WF_PARENT_ELEMENT_ID' =>
null);
394 if ($excludeProductId > 0)
395 $filter[
'!=ID'] = $excludeProductId;
396 $elementQuery->setFilter($filter);
398 $elementQuery->registerRuntimeField(
400 new Main\
Entity\ReferenceField(
402 Main\
Entity\Base::getInstanceByQuery($sectionElementQuery),
403 array(
'=this.ID' =>
'ref.IBLOCK_ELEMENT_ID'),
404 array(
'join_type' =>
'INNER')
408 $query = static::query();
409 $query->setSelect(array(
'PRODUCT_ID',
'ELEMENT_ID',
'DATE_VISIT'));
410 $query->setFilter(array(
'=FUSER_ID' => $fuserId,
'=SITE_ID' => $siteId));
411 $query->setOrder(array(
'DATE_VISIT' =>
'DESC'));
413 $query->registerRuntimeField(
415 new Main\
Entity\ReferenceField(
417 Main\
Entity\Base::getInstanceByQuery($elementQuery),
418 array(
'=this.ELEMENT_ID' =>
'ref.ID'),
419 array(
'join_type' =>
'INNER')
423 $query->setLimit($limit);
425 $iterator = $query->exec();
427 unset($query, $elementQuery, $sectionElementQuery, $subSectionQuery, $sectionQuery);
430 while ($row = $iterator->fetch())
431 $map[$row[
'PRODUCT_ID']] = $row[
'ELEMENT_ID'];
432 unset($row, $iterator);
444 public static function clear($liveTime = 10)
446 $liveTime = (int)$liveTime;
451 $helper = $connection->getSqlHelper();
452 $liveTo = $helper->addSecondsToDateTime($liveTime * 86400, $helper->quote(
'DATE_VISIT'));
455 'delete from '.$helper->quote(self::getTableName()).
456 ' where '.$liveTo.
' < '.$helper->getCurrentDateTimeFunction()
458 unset($liveTo, $helper, $connection);