• Модуль: iblock
  • Путь к файлу: ~/bitrix/modules/iblock/lib/component/elementlist.php
  • Класс: BitrixIblockComponentElementList
  • Вызов: ElementList::chooseOffer
protected function chooseOffer($offers, $iblockId)
{
	if (empty($offers) || empty($this->storage['CATALOGS'][$iblockId]))
		return;
	$uniqueSortHash = array();
	$filteredOffers = array();
	$filteredElements = array();
	$filteredByProperty = $this->getFilteredOffersByProperty($iblockId);

	if (!$this->isMultiIblockMode() && !empty($this->storage['SUB_FILTER']))
	{
		$catalog = $this->storage['CATALOGS'][$iblockId];
		$this->storage['SUB_FILTER']['=PROPERTY_'.$catalog['SKU_PROPERTY_ID']] = array_keys($this->elementLinks);
		$filteredOffers = IblockComponentFilters::getFilteredOffersByProduct(
			$catalog['IBLOCK_ID'],
			$catalog['SKU_PROPERTY_ID'],
			$this->storage['SUB_FILTER']
		);
		unset($catalog);
	}

	foreach ($offers as &$offer)
	{
		$elementId = $offer['LINK_ELEMENT_ID'];

		if (!isset($this->elementLinks[$elementId]))
			continue;

		if (!isset($uniqueSortHash[$elementId]))
		{
			$uniqueSortHash[$elementId] = array();
		}

		$uniqueSortHash[$elementId][$offer['SORT_HASH']] = true;

		if ($this->elementLinks[$elementId]['OFFER_ID_SELECTED'] == 0 && $offer['CAN_BUY'])
		{
			if (isset($filteredOffers[$elementId]))
			{
				if (isset($filteredOffers[$elementId][$offer['ID']]))
				{
					$this->elementLinks[$elementId]['OFFER_ID_SELECTED'] = $offer['ID'];
					$filteredElements[$elementId] = true;
				}
			}
			elseif (isset($filteredByProperty[$elementId]))
			{
				if (isset($filteredByProperty[$elementId][$offer['ID']]))
				{
					$this->elementLinks[$elementId]['OFFER_ID_SELECTED'] = $offer['ID'];
					$filteredElements[$elementId] = true;
				}
			}
			else
			{
				$this->elementLinks[$elementId]['OFFER_ID_SELECTED'] = $offer['ID'];
			}
		}
		unset($elementId);
	}

	if (!empty($filteredOffers))
	{
		$this->arResult['FILTERED_OFFERS_ID'] = array();
	}

	foreach ($this->elementLinks as &$element)
	{
		if (isset($filteredOffers[$element['ID']]))
		{
			$this->arResult['FILTERED_OFFERS_ID'][$element['ID']] = $filteredOffers[$element['ID']];
		}

		if ($element['OFFER_ID_SELECTED'] == 0 || isset($filteredElements[$element['ID']]))
			continue;

		if (count($uniqueSortHash[$element['ID']]) < 2)
		{
			$element['OFFER_ID_SELECTED'] = 0;
		}
	}
}