• Модуль: iblock
  • Путь к файлу: ~/bitrix/modules/iblock/classes/general/cml2.php
  • Класс: CIBlockCMLImport
  • Вызов: CIBlockCMLImport::SetProductPrice
function SetProductPrice($PRODUCT_ID, $arPrices, $arDiscounts = false)
{
	$arDBPrices = array();
	$iterator = CatalogModelPrice::getList(array(
		'select' => array(
			'ID', 'PRODUCT_ID', 'CATALOG_GROUP_ID',
			'QUANTITY_FROM', 'QUANTITY_TO'
		),
		'filter' => array('=PRODUCT_ID' => $PRODUCT_ID)
	));
	while ($row = $iterator->fetch())
	{
		$arDBPrices[$row["CATALOG_GROUP_ID"].":".$row["QUANTITY_FROM"].":".$row["QUANTITY_TO"]] = $row["ID"];
	}

	$arToDelete = $arDBPrices;

	if(!is_array($arPrices))
		$arPrices = array();

	foreach($arPrices as $price)
	{

		if(!isset($price[$this->mess["IBLOCK_XML2_CURRENCY"]]))
			$price[$this->mess["IBLOCK_XML2_CURRENCY"]] = $price["PRICE"]["CURRENCY"];

		$arPrice = Array(
			"PRODUCT_ID" => $PRODUCT_ID,
			"CATALOG_GROUP_ID" => $price["PRICE"]["ID"],
			"^PRICE" => $this->ToFloat($price[$this->mess["IBLOCK_XML2_PRICE_FOR_ONE"]]),
			"CURRENCY" => $this->CheckCurrency($price[$this->mess["IBLOCK_XML2_CURRENCY"]]),
		);

		if (
			(isset($price[$this->mess["IBLOCK_XML2_QUANTITY_FROM"]]) && (string)$price[$this->mess["IBLOCK_XML2_QUANTITY_FROM"]] != '')
			|| (isset($price[$this->mess["IBLOCK_XML2_QUANTITY_TO"]]) && (string)$price[$this->mess["IBLOCK_XML2_QUANTITY_TO"]] != '')
		)
		{
			$arPrice["QUANTITY_FROM"] = $price[$this->mess["IBLOCK_XML2_QUANTITY_FROM"]];
			$arPrice["QUANTITY_TO"] = $price[$this->mess["IBLOCK_XML2_QUANTITY_TO"]];
			$arPrice["PRICE"] = $arPrice["^PRICE"];
			unset($arPrice["^PRICE"]);
			if ($arPrice["QUANTITY_FROM"] === ''
				|| $arPrice["QUANTITY_FROM"] === false
				|| $arPrice["QUANTITY_FROM"] === '0'
				|| $arPrice["QUANTITY_FROM"] === 0
			)
				$arPrice["QUANTITY_FROM"] = null;
			if ($arPrice["QUANTITY_TO"] === ''
				|| $arPrice["QUANTITY_TO"] === false
				|| $arPrice["QUANTITY_TO"] === '0'
				|| $arPrice["QUANTITY_TO"] === 0
			)
				$arPrice["QUANTITY_TO"] = null;

			$id = $arPrice["CATALOG_GROUP_ID"].":".$arPrice["QUANTITY_FROM"].":".$arPrice["QUANTITY_TO"];
			if(isset($arDBPrices[$id]))
			{
				$priceResult = CatalogModelPrice::update($arDBPrices[$id], $arPrice);
				unset($arToDelete[$id]);
			}
			else
			{
				$priceResult = CatalogModelPrice::add($arPrice);
			}
		}
		else
		{
			foreach($this->ConvertDiscounts($arDiscounts) as $arDiscount)
			{
				$arPrice["QUANTITY_FROM"] = $arDiscount["QUANTITY_FROM"];
				$arPrice["QUANTITY_TO"] = $arDiscount["QUANTITY_TO"];
				if($arDiscount["PERCENT"] > 0)
					$arPrice["PRICE"] = $arPrice["^PRICE"] - $arPrice["^PRICE"]/100*$arDiscount["PERCENT"];
				else
					$arPrice["PRICE"] = $arPrice["^PRICE"];
				unset($arPrice["^PRICE"]);
				if ($arPrice["QUANTITY_FROM"] === ''
					|| $arPrice["QUANTITY_FROM"] === false
					|| $arPrice["QUANTITY_FROM"] === '0'
					|| $arPrice["QUANTITY_FROM"] === 0
				)
					$arPrice["QUANTITY_FROM"] = null;
				if ($arPrice["QUANTITY_TO"] === ''
					|| $arPrice["QUANTITY_TO"] === false
					|| $arPrice["QUANTITY_TO"] === '0'
					|| $arPrice["QUANTITY_TO"] === 0
				)
					$arPrice["QUANTITY_TO"] = null;

				$id = $arPrice["CATALOG_GROUP_ID"].":".$arPrice["QUANTITY_FROM"].":".$arPrice["QUANTITY_TO"];
				if(isset($arDBPrices[$id]))
				{
					$priceResult = CatalogModelPrice::update($arDBPrices[$id], $arPrice);
					unset($arToDelete[$id]);
				}
				else
				{
					$priceResult = CatalogModelPrice::add($arPrice);
				}
			}
		}
	}

	foreach($arToDelete as $id)
	{
		$priceResult = CatalogModelPrice::delete($id);
	}
}