• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/discount/prediction/manager.php
  • Класс: BitrixSaleDiscountPredictionManager
  • Вызов: Manager::tryToFindPredictionConnectedProducts
private function tryToFindPredictionConnectedProducts(Basket $basket, array $product, array $templates = []): ?string
{
	if(!$this->checkProductInBasket($product, $basket))
	{
		$this->addProductToBasket($basket, $product);
	}

	$discounts = $this->getDiscounts($basket);
	$predictionDiscount = $this->findFirstPredictionDiscount($discounts, ConnectedProduct::className());
	if ($predictionDiscount === null)
	{
		return null;
	}

	$manager = DiscountPresetManager::getInstance();
	$preset = $manager->getPresetById($predictionDiscount['PRESET_ID']);
	if(!$preset instanceof ConnectedProduct)
	{
		return null;
	}

	$currentProductIds = $this->extendProductIds(array($product['ID']));
	$currentSectionIds = $this->getSectionIdsByProduct($product);

	$state = $preset->generateState($predictionDiscount);
	list($typeConditionProduct, $dataCondition) = $preset->getDescribedDataProductCondition($state);
	list($typeActionProduct, $dataAction) = $preset->getDescribedDataProductAction($state);

	$isAct = $isCond = false;

	if($typeConditionProduct === $preset::TYPE_PRODUCT)
	{
		$condProductIds = $this->extendProductIds($dataCondition);
		if(array_intersect($condProductIds, $currentProductIds))
		{
			$isCond = true;
		}
	}
	elseif($typeConditionProduct === $preset::TYPE_SECTION)
	{
		if(array_intersect($dataCondition, $currentSectionIds))
		{
			$isCond = true;
		}
	}

	if($typeActionProduct === $preset::TYPE_PRODUCT)
	{
		$actProductIds = $this->extendProductIds($dataAction);
		if(array_intersect($actProductIds, $currentProductIds))
		{
			$isAct = true;
		}
	}
	elseif($typeActionProduct === $preset::TYPE_SECTION)
	{
		if(array_intersect($dataAction, $currentSectionIds))
		{
			$isAct = true;
		}
	}

	if(!$isAct && !$isCond)
	{
		return null;
	}

	$predictionText = $preset->getPredictionText(
		$state,
		$isAct? $preset::PREDICTION_TEXT_TYPE_ACTION : $preset::PREDICTION_TEXT_TYPE_CONDITION
	);

	$currencyFormat = '# ' . $predictionDiscount['CURRENCY'];
	if(Loader::includeModule('currency'))
	{
		$currencyFormat = CCurrencyLang::getCurrencyFormat($predictionDiscount['CURRENCY']);
		$currencyFormat = $currencyFormat['FORMAT_STRING'];
	}

	$discountValue = str_replace('#', $state['discount_value'], $currencyFormat);
	if($state['discount_type'] === 'Perc')
	{
		$discountValue = $state['discount_value'] . ' %';
	}

	$placeholders = array(
		'#DISCOUNT_VALUE#' => $discountValue,
	);

	if ($isCond)
	{
		if ($typeActionProduct === $preset::TYPE_SECTION)
		{
			$itemData = $this->getSectionData($dataAction, $templates);
			if (!empty($itemData))
			{
				$placeholders = $placeholders + $itemData;
			}
			unset($itemData);
		}
		if ($typeActionProduct === $preset::TYPE_PRODUCT)
		{
			$itemData = $this->getProductData($dataAction, $templates);
			if (!empty($itemData))
			{
				$placeholders = $placeholders + $itemData;
			}
			unset($itemData);
		}
	}
	elseif ($isAct)
	{
		if ($typeConditionProduct === $preset::TYPE_SECTION)
		{
			$itemData = $this->getSectionData($dataCondition, $templates);
			if (!empty($itemData))
			{
				$placeholders = $placeholders + $itemData;
			}
			unset($itemData);
		}
		if($typeConditionProduct === $preset::TYPE_PRODUCT)
		{
			$itemData = $this->getProductData($dataCondition, $templates);
			if (!empty($itemData))
			{
				$placeholders = $placeholders + $itemData;
			}
			unset($itemData);
		}
	}

	if (empty($placeholders['#NAME#']) || empty($placeholders['#LINK#']))
	{
		return null;
	}

	return str_replace(
		array_keys($placeholders),
		array_values($placeholders),
		$predictionText
	);
}