• Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/discount/discountmanager.php
  • Класс: BitrixCatalogDiscountDiscountManager
  • Вызов: DiscountManager::applyDiscount
static function applyDiscount(&$product, $discount): void
{
	if (empty($product) || !is_array($product))
		return;
	if (empty($discount) || empty($discount['TYPE']))
		return;
	if (isset($discount['CURRENCY']) && $discount['CURRENCY'] != $product['CURRENCY'])
		return;
	if (!isset($product['DISCOUNT_PRICE']))
		$product['DISCOUNT_PRICE'] = 0;
	$getPercentFromBasePrice = (isset($discount['USE_BASE_PRICE']) && $discount['USE_BASE_PRICE'] == 'Y');
	$basePrice = (float)(
		isset($product['BASE_PRICE'])
		? $product['BASE_PRICE']
		: $product['PRICE'] + $product['DISCOUNT_PRICE']
	);

	switch ($discount['TYPE'])
	{
		case CatalogDiscountTable::VALUE_TYPE_PERCENT:
			$discount['VALUE'] = -$discount['VALUE'];
			$discountValue = self::roundValue(
				((
					$getPercentFromBasePrice
						? $basePrice
						: $product['PRICE']
					)*$discount['VALUE'])/100,
				$product['CURRENCY']
			);
			if (isset($discount['MAX_VALUE']) && $discount['MAX_VALUE'] > 0)
			{
				if ($discountValue + $discount['MAX_VALUE'] <= 0)
					$discountValue = -$discount['MAX_VALUE'];
			}
			$product['PRICE'] += $discountValue;
			$product['DISCOUNT_PRICE'] -= $discountValue;
			if (!empty($product['DISCOUNT_RESULT']))
			{
				$product['DISCOUNT_RESULT']['BASKET'][0]['RESULT_VALUE'] = (string)abs($discountValue);
				$product['DISCOUNT_RESULT']['BASKET'][0]['RESULT_UNIT'] = $product['CURRENCY'];
			}
			unset($discountValue);
			break;
		case CatalogDiscountTable::VALUE_TYPE_FIX:
			$discount['VALUE'] = self::roundValue($discount['VALUE'], $product['CURRENCY']);
			$product['PRICE'] -= $discount['VALUE'];
			$product['DISCOUNT_PRICE'] += $discount['VALUE'];
			break;
		case CatalogDiscountTable::VALUE_TYPE_SALE:
			$discount['VALUE'] = self::roundValue($discount['VALUE'], $product['CURRENCY']);
			$product['DISCOUNT_PRICE'] += ($product['PRICE'] - $discount['VALUE']);
			$product['PRICE'] = $discount['VALUE'];
			break;
	}
}