• Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/product/sku.php
  • Класс: BitrixCatalogProductSku
  • Вызов: Sku::updateProductPrices
static function updateProductPrices(array $listIds)
{
	if (empty(self::$calculatePriceTypes))
		return;

	$process = true;

	if (!empty(self::$skuPrices))
	{
		$existIds = array();
		$existIdsByType = array();
		$iterator = CatalogPriceTable::getList(array(
			'select' => array('ID', 'CATALOG_GROUP_ID', 'PRODUCT_ID'),
			'filter' => array('@PRODUCT_ID' => $listIds, '@CATALOG_GROUP_ID' => self::$calculatePriceTypes),
			'order' => array('ID' => 'ASC')
		));
		while ($row = $iterator->fetch())
		{
			$row['ID'] = (int)$row['ID'];
			$priceTypeId = (int)$row['CATALOG_GROUP_ID'];
			$productId = (int)$row['PRODUCT_ID'];
			$existIds[$row['ID']] = $row['ID'];
			if (!isset($existIdsByType[$productId]))
				$existIdsByType[$productId] = array();
			if (!isset($existIdsByType[$productId][$priceTypeId]))
				$existIdsByType[$productId][$priceTypeId] = array();
			$existIdsByType[$productId][$priceTypeId][] = $row['ID'];
		}
		unset($row, $iterator);
		foreach ($listIds as $productId)
		{
			if (!isset(self::$skuPrices[$productId]))
				continue;

			foreach (array_keys(self::$skuPrices[$productId]) as $resultPriceType)
			{
				$rowId = null;
				$row = self::$skuPrices[$productId][$resultPriceType];
				if (!empty($existIdsByType[$productId][$resultPriceType]))
				{
					$rowId = array_shift($existIdsByType[$productId][$resultPriceType]);
					unset($existIds[$rowId]);
				}
				if ($rowId === null)
				{
					$row['PRODUCT_ID'] = $productId;
					$row['CATALOG_GROUP_ID'] = $resultPriceType;
					$rowResult = CatalogPriceTable::add($row);
				}
				else
				{
					$rowResult = CatalogPriceTable::update($rowId, $row);
				}
				if (!$rowResult->isSuccess())
				{
					$process = false;
					break;
				}
			}
		}
		unset($row, $rowResult, $resultPriceType);

		unset($existIdsByType);

		if ($process)
		{
			if (!empty($existIds))
			{
				$conn = MainApplication::getConnection();
				$helper = $conn->getSqlHelper();
				$tableName = $helper->quote(CatalogPriceTable::getTableName());
				foreach (array_chunk($existIds, 500) as $pageIds)
				{
					$conn->queryExecute(
						'delete from '.$tableName.' where '.$helper->quote('ID').' in ('.implode(',', $pageIds).')'
					);
				}
				unset($pageIds);
				unset($helper, $conn);
			}
			unset($existIds);
		}
	}
	else
	{
		$conn = MainApplication::getConnection();
		$helper = $conn->getSqlHelper();
		$conn->queryExecute(
			'delete from '.$helper->quote(CatalogPriceTable::getTableName()).
			' where '.$helper->quote('PRODUCT_ID').' in ('.implode(',', $listIds).')'.
			' and '.$helper->quote('CATALOG_GROUP_ID').' in ('.implode(',', self::$calculatePriceTypes).')'
		);
		unset($helper, $conn);
	}
}