• Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/catalogviewedproduct.php
  • Класс: BitrixCatalogCatalogViewedProductTable
  • Вызов: CatalogViewedProductTable::refresh
static function refresh($productId, $fuserId, $siteId = SITE_ID, $elementId = 0, $recommendationId = '')
{
	$rowId = -1;

	if (Option::get('catalog', 'enable_viewed_products') !== 'Y')
	{
		return $rowId;
	}

	$productId = (int)$productId;
	$fuserId = (int)$fuserId;
	$siteId = (string)$siteId;
	$elementId = (int)$elementId;
	$recommendationId = (string)$recommendationId;
	if ($productId <= 0 || $fuserId <= 0 || $siteId == '')
		return $rowId;

	if (!CatalogProductBasket::isNotCrawler())
		return $rowId;

	$filter = array('=FUSER_ID' => $fuserId, '=SITE_ID' => $siteId);

	$connection = Application::getConnection();
	$helper = $connection->getSqlHelper();

	$sqlSiteId = $helper->forSql($siteId);

	if ($elementId > 0)
	{
		$filter["=ELEMENT_ID"] = $elementId;

		// Delete parent product id (for capability)
		if ($elementId != $productId)
			$connection->query(
				"delete from b_catalog_viewed_product where PRODUCT_ID = ".$elementId." and FUSER_ID = ".$fuserId." and SITE_ID = '".$sqlSiteId."'"
			);
	}
	else
	{
		/** @noinspection PhpMethodOrClassCallIsNotCaseSensitiveInspection */
		$productInfo = CCatalogSku::getProductInfo($productId);
		// Real SKU ID
		if (!empty($productInfo))
		{
			$elementId = $productInfo['ID'];
			$siblings = array();
			// Delete parent product id (for capability)
			$connection->query("delete from b_catalog_viewed_product
								where PRODUCT_ID = ".$productInfo['ID']." and FUSER_ID = ".$fuserId." and SITE_ID = '" .$sqlSiteId. "'"
			);

			$skus = CIBlockElement::getList(
				array(),
				array('IBLOCK_ID' => $productInfo['OFFER_IBLOCK_ID'], '=PROPERTY_'.$productInfo['SKU_PROPERTY_ID'] => $productInfo['ID']),
				false,
				false,
				array('ID', 'IBLOCK_ID')
			);
			/** @noinspection PhpMethodOrClassCallIsNotCaseSensitiveInspection */
			while ($oneSku = $skus->fetch())
				$siblings[] = $oneSku['ID'];
			unset($oneSku, $skus);

			$filter['@PRODUCT_ID'] = $siblings;
		}
		else
		{
			$elementId = $productId;
			$filter['=PRODUCT_ID'] = $productId;
		}
	}

	// recommendation
	if (!empty($elementId))
	{
		global $APPLICATION;

		$recommendationCookie = $APPLICATION->get_cookie(MainAnalyticsCatalog::getCookieLogName());
		if (!empty($recommendationCookie))
		{
			$recommendations = MainAnalyticsCatalog::decodeProductLog($recommendationCookie);
			if (is_array($recommendations) && isset($recommendations[$elementId]))
				$recommendationId = $recommendations[$elementId][0];
		}
	}

	$iterator = static::getList(array(
		'select' => array('ID', 'FUSER_ID', 'DATE_VISIT', 'PRODUCT_ID', 'SITE_ID', 'VIEW_COUNT'),
		'filter' => $filter
	));

	if ($row = $iterator->fetch())
	{
		$update = array(
			"PRODUCT_ID" => $productId,
			"DATE_VISIT" => new MainTypeDateTime,
			'VIEW_COUNT' => $row['VIEW_COUNT'] + 1,
			"ELEMENT_ID" => $elementId
		);
		if (!empty($recommendationId))
			$update["RECOMMENDATION"] = $recommendationId;

		$result = static::update($row['ID'], $update);
	}
	else
	{
		$result = static::add(array(
			"FUSER_ID" => $fuserId,
			"DATE_VISIT" => new MainTypeDateTime(),
			"PRODUCT_ID" => $productId,
			"ELEMENT_ID" => $elementId,
			"SITE_ID" => $siteId,
			"VIEW_COUNT" => 1,
			"RECOMMENDATION" => $recommendationId
		));
	}

	if ($result->isSuccess(true))
	{
		$rowId = $result->getId();
		self::truncateUserViewedProducts($fuserId, $siteId);
	}

	return $rowId;
}