CCrmProduct::Add

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmProduct
  4. Add
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_product.php
  • Класс: \CCrmProduct
  • Вызов: CCrmProduct::Add
static function Add($arFields)
{
	if (!Loader::includeModule('catalog'))
	{
		return false;
	}

	$element = new CIBlockElement();
	$ID = $arFields['ID'] ?? null;
	if ($ID !== null)
	{
		$ID = (int)$ID;
		if ($ID <= 0)
		{
			unset($arFields['ID']);
			$ID = null;
		}
	}
	if ($ID === null)
	{
		//Try to create a CIBlockElement
		$arElement = array();

		if (isset($arFields['CATALOG_ID']))
		{
			$arElement['IBLOCK_ID'] = (int)$arFields['CATALOG_ID'];
		}
		else
		{
			$arFields['CATALOG_ID'] = (int)CCrmCatalog::EnsureDefaultExists();
			$arElement['IBLOCK_ID'] = $arFields['CATALOG_ID'];
		}

		if(isset($arFields['NAME']))
		{
			$arElement['NAME'] = $arFields['NAME'];
		}

		if (isset($arFields['CODE']))
		{
			$arElement['CODE'] = $arFields['CODE'];
		}
		elseif (isset($arElement['NAME']))
		{
			$mnemonicCode = $element->generateMnemonicCode($arElement['NAME'], $arElement['IBLOCK_ID']);
			if ($mnemonicCode !== null)
			{
				$arElement['CODE'] = $mnemonicCode;
			}
		}

		if(isset($arFields['SORT']))
		{
			$arElement['SORT'] = $arFields['SORT'];
		}

		if(isset($arFields['ACTIVE']))
		{
			$arElement['ACTIVE'] = $arFields['ACTIVE'];
		}

		if(isset($arFields['DETAIL_PICTURE']))
		{
			$arElement['DETAIL_PICTURE'] = $arFields['DETAIL_PICTURE'];
		}

		if(isset($arFields['DESCRIPTION']))
		{
			$arElement['DETAIL_TEXT'] = $arFields['DESCRIPTION'];
			$arElement['DETAIL_TEXT_TYPE'] = 'text';
		}

		if(isset($arFields['DESCRIPTION_TYPE']))
		{
			$arElement['DETAIL_TEXT_TYPE'] = $arFields['DESCRIPTION_TYPE'];
		}

		if(isset($arFields['PREVIEW_PICTURE']))
		{
			$arElement['PREVIEW_PICTURE'] = $arFields['PREVIEW_PICTURE'];
		}

		if(isset($arFields['PREVIEW_TEXT']))
		{
			$arElement['PREVIEW_TEXT'] = $arFields['PREVIEW_TEXT'];
			$arElement['PREVIEW_TEXT_TYPE'] = 'text';
		}

		if(isset($arFields['PREVIEW_TEXT_TYPE']))
		{
			$arElement['PREVIEW_TEXT_TYPE'] = $arFields['PREVIEW_TEXT_TYPE'];
		}

		if(isset($arFields['SECTION_ID']))
		{
			$arElement['IBLOCK_SECTION_ID'] = $arFields['SECTION_ID'];
			$arElement['IBLOCK_SECTION'] = array($arElement['IBLOCK_SECTION_ID']);
		}

		if(isset($arFields['XML_ID']))
		{
			$arElement['XML_ID'] = $arFields['XML_ID'];
		}
		else
		{
			if(isset($arFields['ORIGINATOR_ID']) || isset($arFields['ORIGIN_ID']))
			{
				if (isset($arFields['ORIGINATOR_ID']) && isset($arFields['ORIGIN_ID']))
				{
					$arElement['XML_ID'] = $arFields['ORIGINATOR_ID'].'#'.$arFields['ORIGIN_ID'];
				}
				else
				{
					if (isset($arFields['ORIGINATOR_ID'])) $arElement['XML_ID'] = $arFields['ORIGINATOR_ID'].'#';
					else $arElement['XML_ID'] = '#'.$arFields['ORIGIN_ID'];
				}
			}
			else
			{
				if ($arElement['IBLOCK_ID'] != self::getDefaultCatalogId())
					$arElement['XML_ID'] = '#';
			}
		}

		if(isset($arFields['DATE_CREATE']))
		{
			$arElement['DATE_CREATE'] = $arFields['DATE_CREATE'];
		}

		// May be false or null
		if(array_key_exists('TIMESTAMP_X', $arFields))
		{
			$arElement['TIMESTAMP_X'] = $arFields['TIMESTAMP_X'];
		}

		if(isset($arFields['CREATED_BY']))
		{
			$arElement['CREATED_BY'] = $arFields['CREATED_BY'];
		}

		if(isset($arFields['MODIFIED_BY']))
		{
			$arElement['MODIFIED_BY'] = $arFields['MODIFIED_BY'];
		}

		if(isset($arFields['PROPERTY_VALUES']))
		{
			$arElement['PROPERTY_VALUES'] = $arFields['PROPERTY_VALUES'];
		}

		if(!$element->CheckFields($arElement))
		{
			self::RegisterError($element->LAST_ERROR);
			return false;
		}

		$ID = (int)$element->Add($arElement);
		if($ID <= 0)
		{
			self::$LAST_ERROR = $element->LAST_ERROR;
			return false;
		}
		$arFields['ID'] = $ID;
	}

	if (!self::CheckFields('ADD', $arFields, 0))
	{
		$element->Delete($ID);
		return false;
	}

	$arCatalogProductFields = array('ID' => $ID, 'QUANTITY' => 0);
	if (isset($arFields['VAT_INCLUDED']))
		$arCatalogProductFields['VAT_INCLUDED'] = $arFields['VAT_INCLUDED'];
	if (isset($arFields['VAT_ID']) && !empty($arFields['VAT_ID']))
		$arCatalogProductFields['VAT_ID'] = $arFields['VAT_ID'];
	if (isset($arFields['MEASURE']) && !empty($arFields['MEASURE']))
		$arCatalogProductFields['MEASURE'] = (int)$arFields['MEASURE'];
	if (isset($arFields['TYPE']))
	{
		$productType = (int)$arFields['TYPE'];
		$productTypeList = Catalog\ProductTable::getProductTypes(true);
		if (isset($productTypeList[$productType]))
		{
			$arCatalogProductFields['TYPE'] = $productType;
		}
	}
	if (self::innerProductModify($arCatalogProductFields))
	{
		if (isset($arFields['PRICE']))
		{
			self::setPrice(
				$ID,
				$arFields['PRICE'],
				$arFields['CURRENCY_ID'] ?? false
			);
		}
	}
	else
	{
		$element->Delete($ID);
		return false;
	}

	return $ID;
}

Добавить комментарий