• Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/grid/productaction.php
  • Класс: BitrixCatalogGridProductAction
  • Вызов: ProductAction::updateElementList
static function updateElementList(int $iblockId, array $elementIds, array $fields): MainResult
{
	$result = new MainResult();

	if ($iblockId <= 0)
	{
		$result->addError(new MainError(
			Loc::getMessage('BX_CATALOG_PRODUCT_ACTION_ERR_BAD_IBLOCK_ID')
		));
		return $result;
	}
	MainTypeCollection::normalizeArrayValuesByInt($elementIds, true);
	if (empty($elementIds))
	{
		$result->addError(new MainError(
			Loc::getMessage('BX_CATALOG_PRODUCT_ACTION_ERR_EMPTY_ELEMENTS')
		));
		return $result;
	}
	if (empty($fields))
	{
		$result->addError(new MainError(
			Loc::getMessage('BX_CATALOG_PRODUCT_ACTION_ERR_EMPTY_FIELDS')
		));
		return $result;
	}
	$catalog = CCatalogSku::GetInfoByIBlock($iblockId);
	if (empty($catalog))
	{
		$result->addError(new MainError(
			Loc::getMessage('BX_CATALOG_PRODUCT_ACTION_ERR_BAD_CATALOG')
		));
		return $result;
	}

	$allowedTypes = static::getAllowedProductTypes($catalog, $fields);
	if (empty($allowedTypes))
	{
		$result->addError(new MainError(
			Loc::getMessage('BX_CATALOG_PRODUCT_ACTION_ERR_BAD_FIELDS')
		));
		return $result;
	}

	$productResult = self::getProductListByTypeForModify($elementIds, $allowedTypes);
	$data = $productResult->getData();
	$newProducts = $data['NEW_PRODUCT_IDS'] ?? [];
	$existProducts = $data['EXIST_PRODUCT_IDS'] ?? [];
	unset($data);
	if (!$productResult->isSuccess())
	{
		$result->addErrors($productResult->getErrors());
	}
	unset($productResult);

	if (empty($newProducts) && empty($existProducts))
	{
		return $result;
	}

	$data = [
		'fields' => $fields,
		'external_fields' => [
			'IBLOCK_ID' => $iblockId
		]
	];
	foreach ($existProducts as $id)
	{
		$elementResult = CatalogModelProduct::update($id, $data);
		if (!$elementResult->isSuccess())
		{
			$result->addError(new MainError(
				implode('; ', $elementResult->getErrorMessages()),
				$id
			));
		}
	}
	foreach ($newProducts as $id)
	{
		$data['fields']['ID'] = $id;
		$elementResult = CatalogModelProduct::add($data);
		if (!$elementResult->isSuccess())
		{
			$result->addError(new MainError(
				implode('; ', $elementResult->getErrorMessages()),
				$id
			));
		}
	}
	unset($elementResult, $id, $data);
	unset($newProducts, $existProducts);
	unset($blackList, $catalog);

	return $result;
}